C++ Default Assignment Operator

Discussion in 'C++' started by Ganesh Rajaraman, Jan 3, 2007.

  1. Hi,

    This is the program that i am trying.

    class A
    {
    public:
    A()
    {
    cout<<"Default Constructor"<<endl;
    a=0;
    }
    A(void *arg)
    {
    cout<<"I am into
    Constructor(Void *)<<endl;
    cout<<"Value Of A : "<<*((int *)arg)<<endl;
    a= *((int *)arg);
    }
    int getA()
    {
    return a;
    }
    void setA(int arg)
    {
    a=arg;
    }

    ~A()
    {
    cout << "Inside destructor" << endl;
    }

    private:
    int a;
    };

    int main()
    {
    A obj;
    obj=NULL;
    return 1;
    }

    Output :

    Default Constructor
    I am into Constructor(Void *)
    Segmentation Fault(Core Dumped)



    Here I have a default Constructor and a single Argument Constructor
    which takes a void * (I dont see any reason for having this but juz to
    explain this scenario).

    In my main the instance of A "obj" gets constructed by the default
    constructor. When I try to assign "obj=NULL" i expect a compilation
    error. Instead it compiles fine and it tries to call my single argument
    constructor "A( void *)" and gives me a "core dump" as it is a NULL
    pointer. when I say "obj=NULL" i expect the default assignment operator
    method to be called but how is my constructor call happens.

    I tried this in Linux with g++ compiler.


    Question is
    1. when i try to assign a obj with a NULL why i am not getting a
    compilation error.
    2. why is A(void *) getting invoked.
    3. How is C++ Default Assignment Operator Implemented.

    Thanks,
    Ganesh.
     
    Ganesh Rajaraman, Jan 3, 2007
    #1
    1. Advertising

  2. Ganesh Rajaraman

    red floyd Guest

    Ganesh Rajaraman wrote:
    > Hi,
    >
    > This is the program that i am trying.
    >
    > class A
    > {
    > public:
    > A()
    > {
    > cout<<"Default Constructor"<<endl;
    > a=0;
    > }
    > A(void *arg)
    > {
    > cout<<"I am into
    > Constructor(Void *)<<endl;
    > cout<<"Value Of A : "<<*((int *)arg)<<endl;
    > a= *((int *)arg);
    > }
    > int getA()
    > {
    > return a;
    > }
    > void setA(int arg)
    > {
    > a=arg;
    > }
    >
    > ~A()
    > {
    > cout << "Inside destructor" << endl;
    > }
    >
    > private:
    > int a;
    > };
    >
    > int main()
    > {
    > A obj;
    > obj=NULL;
    > return 1;
    > }
    >
    > Output :
    >
    > Default Constructor
    > I am into Constructor(Void *)
    > Segmentation Fault(Core Dumped)
    >[redacted]
    >
    >
    > Question is
    > 1. when i try to assign a obj with a NULL why i am not getting a
    > compilation error.

    Because the compiler silently translates
    obj=NULL;

    into
    obj = A(NULL);

    > 2. why is A(void *) getting invoked.

    See above

    > 3. How is C++ Default Assignment Operator Implemented.

    See above.
     
    red floyd, Jan 3, 2007
    #2
    1. Advertising

  3. Ganesh Rajaraman wrote:

    > Question is
    > 1. when i try to assign a obj with a NULL why i am not getting a
    > compilation error.
    > 2. why is A(void *) getting invoked.


    Because you have a constructor that takes a void * and is not marked as
    explicit. A non explicit constructor with one argument can be used by the
    compiler as an implicit conversion operator.

    So the line:

    obj= NULL;

    becomes:

    obj= A (NULL);

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 4, 2007
    #3
  4. Ganesh Rajaraman

    Grizlyk Guest

    Ganesh Rajaraman wrote:

    > Hi,
    >
    >A(void *arg)
    >{
    > cout<<"I am into Constructor(Void *)<<endl;
    > cout<<"Value Of A : "<<*((int *)arg)<<endl;
    > a= *((int *)arg);
    >}
    >
    > A obj;
    > obj=NULL;
    >
    > Default Constructor
    > I am into Constructor(Void *)
    > Segmentation Fault(Core Dumped)


    Segmentation Fault because
    a= *((int *)arg);
    for (arg == NULL) is equal to
    a= *((int*)0) =* 0 = segmentation fault due to access (read) from
    address 0.

    It is implementation depended, in some systems (OSes and compilers)
    access to address 0 treats as segmentation fault, even for DOS, one can
    see "NULL pointer assignment" message after program exit.

    Check "arg" for example like this:
    a= arg? *((int*)arg): 0;
     
    Grizlyk, Jan 4, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    492
    Barry Schwarz
    Feb 8, 2005
  2. Chris
    Replies:
    34
    Views:
    1,532
  3. George2
    Replies:
    9
    Views:
    450
    Tadeusz B. Kopec
    Dec 16, 2007
  4. puzzlecracker
    Replies:
    8
    Views:
    434
    James Kanze
    Apr 15, 2008
  5. Hicham Mouline
    Replies:
    2
    Views:
    699
    Juha Nieminen
    Sep 1, 2009
Loading...

Share This Page