Showing posts with label Pointers and Constants. Show all posts
Showing posts with label Pointers and Constants. Show all posts

Sunday, December 2, 2012

Pointers, References and Local Variables

Well, I bring here an example when you should figure out how constructors, pointers and reference works.

Thus, here it goes:

Note: Take a fast look to A class and start reading in main function.

#include <iostream>
#include <algorithm>
#include <stdlib.h>

using namespace std;

class A{
private:
    int a;
public:
    A() : a(-1){
        cout << "Default constructor" << endl;
    }
    A(int val) : a(val){
        cout << "Calling contructor by int" << endl;
    }
    //************************************
    A(A* val){
        cout << "Calling contructor by A_pointer" << endl;
        a = val->get();
    }
    A(A& val){
        cout << "Calling contructor by A_reference" << endl;
        a = val.get();
    }

    // This can't be done because when the constructor is called
    // the parameter of the function calls itself, I mean, val is being
    // a local variable
    /*A(A val){
        a = val.get();
    }*/

    int get(){ return a; }
    int get() const { cout << "get 'a' by a const variable\n"; return a; }
    int& getRef(){ return a; }
    //int& getRef() const{ return a; } This can't be done
};


void byPointers(A *a, A *b){
    *a = b->get()*2; //(-1 * 2 = -2)
    // this can be done because a points to a instanced class A
    // in case that would point to null this would be a segmentacion fault
    // on runtime

    // Beside, this calls the constructor by value
}

void byReference(A& a, const A& b){
    a.getRef() = 1000; // we change a_int value, because a is a reference and a.getRef return the atribut inside the class by reference
                       // if the int atribut would've been public that would be equivalent to a.a = 1000;

    //so, because in this call, a and b are A_value, if we change a, also are changing b, let's look:
    cout << b.get() << endl;

    //However, we can't change the b value, because is constant although points to the same variable
}
void byReference2(A& a){
    a.getRef() = 1000000;
}

void byCopy(A a){
    cout << "a: " << a.get() << endl;

    a = A(25); 
    cout << "a: " << a.get() << endl; // now it's 25

    a.getRef() = 26;
    cout << "a: " << a.get() << endl; // now it's 26 but still being
    // a local variable
}

int main(){
    cout << "First example:" << endl;
    A A_value; // this calls default constructor
    
    cout << A_value.get() << endl;
    A* A_pointer; // this doesn't because is a pointer
    A_pointer = &A_value; // this doesn't call any A constructor
    *A_pointer = A_value; // this is equivialent to the previous line
    cout << A_pointer->get() << ", " << (*A_pointer).get() << endl << endl; // point to A_value (two ways to access it)
    //**************************************************************************************

    cout << "Second example:" << endl;
    byPointers(&A_value,&A_value); // this doesn't call any constructor
    // just points to the references of A_value, so inside the function 
    // if we change A *a, A *b pointers we'll change A_value
    cout << endl;

    // This is a bit obfuscated, so peruse
    byCopy(A_value); // byCopy expect to creat (A a) as a local variable
                     // so calls A_reference, or method to copy a class
                     // however, the construtor is by reference, a still be
                     // a local variable
    cout << A_value.get() << endl << endl;// Thus, we haven't changed any attribut of A_value
    //**************************************************************************************
    
    cout << "Third example:" << endl;
    byReference(A_value, A_value); // pass by reference doesn't call any constructor
                                   // because a and b are exactly A_value
    // So, after the call, A_value have changed:
    cout << A_value.get() << endl;

    // even, we can do the same with the pointer
    byReference2(*A_pointer);
    // and because A_pointer points to A_value, this will also change the value
    cout << A_value.get() << endl; 

    system("pause");
    return 0;
}

Monday, March 5, 2012

Pointers and Constants using Int

Hello everybody,

I did a short example to give an expression of pointers and constants. It's uniquely illustrative. With all this, I'm trying to lay the groundwork to const correctness's use because the good use of const is absolutely important in complex applications where the efficient matter.

Here we go:


int main(){
           int         intInt;
     const int         cint = 10;//we have to initialize here unless 
                                 that is an atribut's class
                                 //and, in this case, have to be 
                                 initialize inside the constructor
           int *       ptrInt;
     const int *       constValue;
           int * const constPtr = &intInt;//have to be initialize here
     const int * const constPtrValue = &intInt;//have to be initialize 
                                               here

     
     //intInt
     intInt = 1;
     intInt = 2;
     //we can change the value as times as we like
     
     //cint
     //cint = 2; EROR: this variable is const so is the same 
     to say read-only
     
     //ptrInt
     ptrInt = &intInt;
     //ptrInt = &cint; ERROR: the reference from cint is 'const int*' 
     and was expected int*
     *ptrInt = cint; //we can put the value of cint inside our pointer
                     //but in this case we have copied by value and no 
                     by reference so
                     //*ptrInt don't point to a const and then 
                     we can increment his value:
     (*ptrInt)++;    //now is 11
     
     //constant pointer
     constValue = &cint; //we can point to any constant int
     int aux = 20;
     constValue = &aux; //even to a int but still being unaccesible 
                        using constValue
     intInt = 30;
     int *auxPtr = &intInt;
     constVconstValue = auxPtr;//in this way the value of constValue is 30
                         //and still being unaccesible from constValue
     //*constValue = 12; ERROR: the value of constValue is const, 
     so *constValue is read-only
     intInt = 100;       //but we can change the value from the variable
                         //so this is legal, and then
     std::cout << *constValue;//this print 100
     
     (*auxPtr)++;        //but we still having access also from our auxPtr
                         //so this is legal to
     std::cout << *constValue;//and now, this print 101
     
     //int * const constPtr
     //constPtr = auxPtr; EROR: our pointer is constant so we can't change 
     the direction where point
     *constPtr = 2;//but the value isn't constant, so we can change it
     std::cout << *constPtr; // is 2
     std::cout << intInt;// and the value have been changed too, is 2
     
     //const int * const constPtrValue
     std::cout << *constPtrValue;// is 2 because we are pointing to intInt
     //constPtrValue = ptrInt; ERROR: assigment of read-only variable
     //*constPtrValue = 20; ERROR: read-only value
                 //so we only have access to the variable
                 //although we can change the value with other variables
     intInt = 666;
     std::cout << *constPtrValue; //this print 666
}

Friday, February 17, 2012

Pointers and Constants

Hello everybody,
This is a fast example to give expression to pointer and constants.

I had some questions about char const* const, so now, I try to explain what exactly means.

If you continue having questions don't doubt to ask me.

#include <iostream>

int main(){    

     char const* s1 = "Testing";//pointer to const variable
     char* const s2 = "constant";//const pointer
     char const* const s3 = "aloha";//const pointer and variable

     s1 = "testing 2";//works perfectly, reassigning the pointer to another const variable
     //*(s1+1) = 'A'; error, tring to change Testing to TAsting
     //*s1 is const, so can't be modify
     
      //s2 = "new string"; error, read-only, you can't change the pointer because is constant
     *s2 = 'A';//you can modify the value

     //s3 = "fuuuu"; error, constant pointer
     //*s3 = 'B'; error, constant value
     

     return 0;

}

Thanks you all!