Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > copy-initialization and class object as static member

Reply
Thread Tools

copy-initialization and class object as static member

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      11-13-2007
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: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: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

 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      11-13-2007
http://www.velocityreviews.com/forums/(E-Mail Removed), 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: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:bj = 9;


This is equivalent to
Base Base: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
 
Reply With Quote
 
 
 
 
Tadeusz B. Kopec
Guest
Posts: n/a
 
      11-13-2007
On Tue, 13 Nov 2007 04:09:04 -0800, (E-Mail Removed), 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: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: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 ((E-Mail Removed))
The road to Hades is easy to travel.
-- Bion
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Does a static variable in a class's member fn always remain static? Sam C++ 4 01-13-2004 11:05 PM



Advertisments