473,657 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy-initialization and class object as static member

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(cons t Base & arg) : val(arg.val)
{
cout << "copy ctor invoked" << endl;
}

Base Base::obj = 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::obj = 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

Nov 13 '07 #1
2 1827
su************* *@yahoo.com, 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(cons t Base & arg) : val(arg.val)
{
cout << "copy ctor invoked" << endl;
}

Base Base::obj = 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::obj = 9;
This is equivalent to
Base Base::obj(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
Nov 13 '07 #2
On Tue, 13 Nov 2007 04:09:04 -0800, su************* *@yahoo.com, 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::obj = 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::obj = 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 (tk****@NOSPAMP LEASElife.pl)
The road to Hades is easy to travel.
-- Bion
Nov 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
4659
by: Claudio Grondi | last post by:
I would like to save time copying the same file (>6 GByte) to various different target storage media connected to the system via USB. Is there a (Python or other) tool able to help me to do this, so that I don't need to copy the source file first to the first media, then to the second, etc.? Claudio
15
21186
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including pointers) and for objects types call their default constructor. Any others points i should know?
7
1721
by: Richard Forester | last post by:
Hello. I need some help understanding what goes on when an array is copied. I create 2 arrays and copy one to the other: int pins = {9, 3, 7, 2}; int copy = new int; for (int i = 0; i != copy.Length; i++)
3
2181
by: Johnny | last post by:
Hi, I have created an ASP.NET application (let's call it FooBar) with VS.NET on my local machine, residing in http://localhost/FooBar. Deploying it to another folder on my machine works well with FrontPage Server Extensions, but I don't manage to deploy the application to my web provider http root folder. When I run Copy Project, I get an error message just saying 'Unable to create Web project 'myname'. Not found.'.
2
1639
by: Steve Franks | last post by:
The Copy Web tool provided with VS.NET 2005 is very convenient. However every once in a while it seems to think the files on the remote server have changed, which they have not. Then when I use the Copy Web Site feature of Copy Web to just copy the entire web site over, it thinks it has to re-upload the entire site which takes a very long time - when in fact those files on the remote have not changed. Does anyone know why this occurs...
8
2921
by: luis molina Micasoft | last post by:
it seems that when i do file.copy the svchost.exe is hanged, i mean if i make 40 threads of file.copy , 40 copys of files at same time the system is going down and stop responding, this is when i'm working with cifs (shares). there is another solution to copy files than file.copy in .net?
1
3532
by: Rachel McConnell | last post by:
Hi, I am trying to import data using COPY, from a file containing thirty or so COPY commands each with 0 or more rows of data. Reason, I have a small data set I want to include into a database with an identical schema, with existing data. I figured a good way to do this would be to use pg_dump on the small dataset, trim out the table definitions and constraints, and run the resulting file. (I also reordered the COPY statements to...
4
5426
by: jiang.haiyun | last post by:
Hello all, when i import SOAPpy, the python crashed and print out 'usage:copy source destination'. As follows: ############################ haiyun# python Python 2.4.1 (#2, Mar 28 2006, 21:00:14) 20040728] on freebsd5 Type "help", "copyright", "credits" or "license" for more information. usage:copy source destination
3
12837
by: maheshkadam | last post by:
Hi friends I am new to perl so please guide me. I have one application which created backup log file every day.But it appends that file so you can see logs for different day in one file only. My requirement is to copy backup log for the specific day (yesterday) and write in other file. That file will be mailed to admin for ready reference. So here is some text from that log file
0
8384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8820
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8499
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.