Consider the following program:
#include <iostream>
using namespace std;
class Test
{
static Test t;
static Test init_Test( ) { return t; }
Test(const Test & rhs) { cout << "copy ctor" << endl; }
};
Test Test::t = init_Test( );
int main()
{
return 0;
}
This program compiles fine under both g++ and VC++2005 Express Edition
and both produce the following same output
copy ctor
However consider the statement
Test Test::t = init_Test( );
Here init_Test( ) is called which returns the static member object
under construction which is 't'. I do not understand how we can
return an object which is still under construction.
How is it accepted by the compiler ?
Kindly explain
Thanks
V.Subramanian 16 3016
Try putting in a default constructor that writes some output. I think
that will show you what's going on. Also, you're not returning t
you're returning a copy of t.
On Sep 25, 12:47 pm, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
Consider the following program:
#include <iostream>
using namespace std;
class Test
{
static Test t;
static Test init_Test( ) { return t; }
Test(const Test & rhs) { cout << "copy ctor" << endl; }
};
Test Test::t = init_Test( );
int main()
{
return 0;
}
This program compiles fine under both g++ and VC++2005 Express Edition
and both produce the following same output
copy ctor
However consider the statement
Test Test::t = init_Test( );
Here init_Test( ) is called which returns the static member object
under construction which is 't'. I do not understand how we can
return an object which is still under construction.
How is it accepted by the compiler ?
Kindly explain
Thanks
V.Subramanian
su************* *@yahoo.com wrote:
Consider the following program:
#include <iostream>
using namespace std;
class Test
{
static Test t;
static Test init_Test( ) { return t; }
Test(const Test & rhs) { cout << "copy ctor" << endl; }
};
Test Test::t = init_Test( );
int main()
{
return 0;
}
This program compiles fine under both g++ and VC++2005 Express Edition
and both produce the following same output
copy ctor
However consider the statement
Test Test::t = init_Test( );
Here init_Test( ) is called which returns the static member object
under construction which is 't'. I do not understand how we can
return an object which is still under construction.
How is it accepted by the compiler ?
How should the compiler be able to catch your *logical* errors? The
compiler is not obligated to tell you your logic is incorrect. Or
that you have a chicken and egg problem. The compiler just checks
the syntax and types. Your code has undefined behaviour since it
makes an attempt to use an uninitialised object. The same thing as
here:
int &r = r;
I think the compiler is not required to find and flag all instances of
undefined behaviour in your code.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask su************* *@yahoo.com, India wrote:
Consider the following program:
#include <iostream>
using namespace std;
class Test
{
static Test t;
static Test init_Test( ) { return t; }
Test(const Test & rhs) { cout << "copy ctor" << endl; }
};
Test Test::t = init_Test( );
int main()
{
return 0;
}
This program compiles fine under both g++ and VC++2005 Express Edition
and both produce the following same output
copy ctor
However consider the statement
Test Test::t = init_Test( );
Here init_Test( ) is called which returns the static member object
under construction which is 't'. I do not understand how we can
return an object which is still under construction.
How is it accepted by the compiler ?
This is stunning, and even puzzling that "init_Test( )' compiles, which
should be Test::init_Test (); to be well-formed, I think
Comeau online also accepts all the cases.
Waiting guru for explanation.
--
Thanks
Barry
Barry wrote:
su************* *@yahoo.com, India wrote:
>Consider the following program:
#include <iostream>
using namespace std;
class Test { static Test t; static Test init_Test( ) { return t; } Test(const Test & rhs) { cout << "copy ctor" << endl; } };
Test Test::t = init_Test( );
int main() { return 0; }
This program compiles fine under both g++ and VC++2005 Express Edition and both produce the following same output copy ctor
However consider the statement Test Test::t = init_Test( ); Here init_Test( ) is called which returns the static member object under construction which is 't'. I do not understand how we can return an object which is still under construction. How is it accepted by the compiler ?
This is stunning, and even puzzling that "init_Test( )' compiles, which
should be Test::init_Test (); to be well-formed, I think
Comeau online also accepts all the cases.
Waiting guru for explanation.
Since you're defining 't' that is declared inside 'Test', 'Test' scope
is also looked up. I am sure you can find it somewhere in subclause
3.4 (name lookup).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Victor Bazarov wrote:
su************* *@yahoo.com wrote:
>Consider the following program:
#include <iostream>
using namespace std;
class Test { static Test t; static Test init_Test( ) { return t; } Test(const Test & rhs) { cout << "copy ctor" << endl; } };
Test Test::t = init_Test( );
int main() { return 0; }
This program compiles fine under both g++ and VC++2005 Express Edition and both produce the following same output copy ctor
However consider the statement Test Test::t = init_Test( ); Here init_Test( ) is called which returns the static member object under construction which is 't'. I do not understand how we can return an object which is still under construction. How is it accepted by the compiler ?
How should the compiler be able to catch your *logical* errors? The
compiler is not obligated to tell you your logic is incorrect. Or
that you have a chicken and egg problem. The compiler just checks
the syntax and types. Your code has undefined behaviour since it
makes an attempt to use an uninitialised object. The same thing as
here:
int &r = r;
I think the compiler is not required to find and flag all instances of
undefined behaviour in your code.
Well, the code from the OP, do have the "chicken and egg" problem, while
this one does not:
class A
{
static A a;
static A Init() { return A(); }
A() {}
};
A A::a = A::Init();
// or even
//A A::a = Init();
int main()
{
}
the code above compiles with Comeau online
--
Thanks
Barry
On Sep 25, 2:02 pm, Barry <dhb2...@gmail. comwrote:
Victor Bazarov wrote:
subramanian10.. .@yahoo.com wrote:
Consider the following program:
#include <iostream>
using namespace std;
class Test
{
static Test t;
static Test init_Test( ) { return t; }
Test(const Test & rhs) { cout << "copy ctor" << endl; }
};
Test Test::t = init_Test( );
int main()
{
return 0;
}
This program compiles fine under both g++ and VC++2005 Express Edition
and both produce the following same output
copy ctor
However consider the statement
Test Test::t = init_Test( );
I would think that:
Test Test::t <- this part runs the default ctor
= init_Test() <- this part then calls operator=() on the newly
constructed Test object. init_Test then returns t by value so you end
up with a statement like:
Test Test::t = Test(Test::t);
Or am I missing the point here?
Jonathan Lane wrote:
On Sep 25, 2:02 pm, Barry <dhb2...@gmail. comwrote:
>Victor Bazarov wrote:
>>subramanian10 ...@yahoo.com wrote: Consider the following program: #include <iostream> using namespace std; class Test { static Test t; static Test init_Test( ) { return t; } Test(const Test & rhs) { cout << "copy ctor" << endl; } }; Test Test::t = init_Test( ); int main() { return 0; } This program compiles fine under both g++ and VC++2005 Express Edition and both produce the following same output copy ctor However consider the statement Test Test::t = init_Test( );
I would think that:
Test Test::t <- this part runs the default ctor
= init_Test() <- this part then calls operator=() on the newly
constructed Test object. init_Test then returns t by value so you end
up with a statement like:
Test Test::t = Test(Test::t);
Or am I missing the point here?
My point is that whichever "Init()" or "A::Init()" is private member
function, why access control does not apply here.
--
Thanks
Barry
Jonathan Lane wrote:
On Sep 25, 2:02 pm, Barry <dhb2...@gmail. comwrote:
>Victor Bazarov wrote:
>>subramanian10 ...@yahoo.com wrote: Consider the following program:
>>>#include <iostream>
>>>using namespace std;
>>>class Test { static Test t; static Test init_Test( ) { return t; } Test(const Test & rhs) { cout << "copy ctor" << endl; } };
>>>Test Test::t = init_Test( );
>>>int main() { return 0; }
>>>This program compiles fine under both g++ and VC++2005 Express Edition and both produce the following same output copy ctor
>>>However consider the statement Test Test::t = init_Test( );
I would think that:
Test Test::t <- this part runs the default ctor
= init_Test() <- this part then calls operator=() on the newly
constructed Test object. init_Test then returns t by value so you end
up with a statement like:
Test Test::t = Test(Test::t);
Or am I missing the point here?
Not only you're missing the point. You're also missing the fact that
'init_Test' is a function and not an object. And you're also missing
the fact that the statement
Blah Blah::blah = blahblah();
has no default constructor involved at all. It's called "copy-
initialisation" and relates to copy construction. 'blahblah' would
return a temporary (whatever way is used to construct it), and then
the 'blah' object is copy-constructed from that temporary.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jonathan Lane wrote:
On Sep 25, 2:02 pm, Barry <dhb2...@gmail. comwrote:
>Victor Bazarov wrote:
>>subramanian10 ...@yahoo.com wrote: Consider the following program: #include <iostream> using namespace std; class Test { static Test t; static Test init_Test( ) { return t; } Test(const Test & rhs) { cout << "copy ctor" << endl; } }; Test Test::t = init_Test( ); int main() { return 0; } This program compiles fine under both g++ and VC++2005 Express Edition and both produce the following same output copy ctor However consider the statement Test Test::t = init_Test( );
I would think that:
Test Test::t <- this part runs the default ctor
= init_Test() <- this part then calls operator=() on the newly
constructed Test object. init_Test then returns t by value so you end
up with a statement like:
Test Test::t = Test(Test::t);
Or am I missing the point here?
class A
{
static A a;
static int i;
static A Init() { return A(); }
static int GetInt() {return 10;}
A() {}
};
A A::a = A::Init();
int A::i = A::GetInt();
int main()
{
}
well, I think I got a point,
the initialization of static member data(no matter private/public), the
initialization statement creates a scope, in this special scope, all
member functions can be called.
I don't know whether Victor meant this else thread.
But if we initiate a global object of A like this:
A a1 = A::Init();
No way, A::Init() is private.
--
Thanks
Barry This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: cppaddict |
last post by:
I have class with two static member objects, one of type int and one
of type vector<int>.
static int myStaticMemberInt
static vector<int> myStaticMemberVector;
I know how to initialize the int member:
MyClass::myStaticMemberInt = 99;
|
by: DanielBradley |
last post by:
Hello all,
I have recently been porting code from Linux to cygwin and came across
a problem with static const class members (discussed below). I am
seeking to determine whether I am programming non-standard C++ or if the
problem lies elsewhere.
To summarize static const class members are not being accessed properly
when accessed from a DLL by another external object file (not within the
DLL). It only occurs when the static const...
|
by: Tran Tuan Anh |
last post by:
Dear all,
I am new with C++ and very confused with some features.
Really appreciate if you can explain to me some of stuffs below.
I define a class:
class A {
static A* instance = 0;
};
|
by: Mike - EMAIL IGNORED |
last post by:
MyClass
{
//I have a static member method:
void static myMethod();
//and a static data member:
static MyType myData;
};
//In the .cpp file:
void MyClass::myMethod()
|
by: Per Bull Holmen |
last post by:
Hey
Im new to c++, so bear with me. I'm used to other OO languages, where it
is possible to have class-level initialization functions, that
initialize the CLASS rather than an instance of it. Like, for instance
the Objective-C method:
+(void)initialize
Which has the following characteristics: It is guaranteed to be run
| |
by: Spoon |
last post by:
Hello everyone,
I have a Packet class I use to send packets over the Internet. All the
packets sent in a session are supposed to share a common random ID.
I figured I'd use a static const member inside my class.
class Packet
{
static const int session_id;
|
by: Bram Kuijper |
last post by:
Hi all,
as a C++ newbie, I got some question on the initialization of static
reference data members.
Since it isn't possible to initialize static members of a class in the
constructor, I should initialize them in advance. However, the following
code, in which I first produce two classses and then try to assign a
reference of the first class to a static data member of the second class
doesn't work. It gives the following compiler error:
|
by: subramanian100in |
last post by:
Consider the following program:
#include <iostream>
using namespace std;
class Base
{
public:
Base(int x = 0);
|
by: gs |
last post by:
Hi,
I want to know that when memory get allocated to static data member of
a class.
class A
{
static int i;
}
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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,...
| |
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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...
| |