copy-initialization and class object as static member

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Nov 13, 2007.

  1. , India

    , India Guest

    Consider the following program:

    #include <iostream>

    using namespace std;

    class Base
    {
    public:
    Base(int x = 0);

    private:
    Base(const Base & arg);

    int val;
    static Base obj;
    };

    Base::Base(int x) : val(x)
    {
    cout << "one arg ctor called" << endl;
    }

    Base::Base(const Base & arg) : val(arg.val)
    {
    cout << "copy ctor invoked" << endl;
    }

    Base Base::eek:bj = 9;

    int main()
    {
    Base x = 1; // copy-initialization

    return 0;
    }

    Suppose the above program is named x.cpp

    When I compile this program under g++ with
    g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

    I get compilation error for the following line in main():
    Base x = 1;

    This is because the copy ctor is private and copy-initialization is
    involved.
    But I do not get this error for the line:
    Base Base::eek:bj = 9;

    Why is the copy-initialization of class object as static member
    treated
    differently? I do not understand the difference.
    Where I am going wrong ?

    Kindly explain.

    Thanks
    V.Subramanian
    , India, Nov 13, 2007
    #1
    1. Advertising

  2. , India a écrit :
    > Consider the following program:
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > class Base
    > {
    > public:
    > Base(int x = 0);
    >
    > private:
    > Base(const Base & arg);
    >
    > int val;
    > static Base obj;
    > };
    >
    > Base::Base(int x) : val(x)
    > {
    > cout << "one arg ctor called" << endl;
    > }
    >
    > Base::Base(const Base & arg) : val(arg.val)
    > {
    > cout << "copy ctor invoked" << endl;
    > }
    >
    > Base Base::eek:bj = 9;
    >
    > int main()
    > {
    > Base x = 1; // copy-initialization
    >
    > return 0;
    > }
    >
    > Suppose the above program is named x.cpp
    >
    > When I compile this program under g++ with
    > g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
    >
    > I get compilation error for the following line in main():
    > Base x = 1;


    This is equivalent to:
    Base x(Base(1));

    And since copy constructor is private, you get an error.

    >
    > This is because the copy ctor is private and copy-initialization is
    > involved.
    > But I do not get this error for the line:
    > Base Base::eek:bj = 9;


    This is equivalent to
    Base Base::eek:bj(Base(9));

    Here, since obj is a member of Base, it has access to private function
    and can call copy constructor.

    >
    > Why is the copy-initialization of class object as static member
    > treated
    > differently? I do not understand the difference.
    > Where I am going wrong ?



    Michael
    Michael DOUBEZ, Nov 13, 2007
    #2
    1. Advertising

  3. On Tue, 13 Nov 2007 04:09:04 -0800, , India
    wrote:

    > Consider the following program:
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > class Base
    > {
    > public:
    > Base(int x = 0);
    >
    > private:
    > Base(const Base & arg);
    >
    > int val;
    > static Base obj;
    > };


    [snip]

    > Base Base::eek:bj = 9;
    >
    > int main()
    > {
    > Base x = 1; // copy-initialization
    >
    > return 0;
    > }
    >
    > Suppose the above program is named x.cpp
    >
    > When I compile this program under g++ with g++ -std=c++98 -pedantic
    > -Wall -Wextra x.cpp
    >
    > I get compilation error for the following line in main(): Base x = 1;
    >
    > This is because the copy ctor is private and copy-initialization is
    > involved.
    > But I do not get this error for the line: Base Base::eek:bj = 9;
    >
    > Why is the copy-initialization of class object as static member treated
    > differently? I do not understand the difference. Where I am going wrong
    > ?


    Notice that the static member is also private. During initialisation of
    static members you have the access to all private and protected data.

    Also keep in mind that for initialisations like
    Base x = 1;
    the compiler is required only to check the accessibility of copy
    constructor but is free to optimise away the call to it.

    --
    Tadeusz B. Kopec ()
    The road to Hades is easy to travel.
    -- Bion
    Tadeusz B. Kopec, Nov 13, 2007
    #3
    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. Siemel Naran
    Replies:
    4
    Views:
    801
    Micah Cowan
    Jan 12, 2005
  2. , India
    Replies:
    16
    Views:
    578
    Jonathan Lane
    Sep 26, 2007
  3. dolphin
    Replies:
    3
    Views:
    1,338
    Pete Becker
    Dec 5, 2007
  4. Replies:
    5
    Views:
    1,970
    Arne Mertz
    Mar 26, 2009
  5. Taras_96
    Replies:
    3
    Views:
    583
    Michael Tsang
    Oct 30, 2009
Loading...

Share This Page