object assignment/initialisation question

A

Anonymous

Just need to clarify the order at which assignement takes place

class A
{
public:
A(){ std::cout << "default ctor"; }
A(const A& a){ std::cout << "copy ctor"; }
A& operator= (const A& a){ std::cout << "assignment operator"; }
~A(){}

};


int main(int argc, char* argv[])
{
A a ; // default ctor
A b(a) ; // copy ctor
A c = b ; // default ctor THEN assignemnt OR copy ctor ???
}


OT:
I am using the clunky MSVC IDE, so am unable to quickly compile and test
this (without creating a project in the IDE) - incidentally, if anyone
knows how to compile litle code snippets like this using a command line
compiler that runs on Windoze (possibly the VC complier?) - without
needing invoking the IDE/ requiring a project etc, ala gcc - please post
instructions etc, so in future I can carry out such tests myself )
 
N

Neelesh Bodas

Just need to clarify the order at which assignement takes place

int main(int argc, char* argv[])
{
A a ; // default ctor
A b(a) ; // copy ctor
A c = b ; // default ctor THEN assignemnt OR copy ctor ???

copy constructor.

A c;
c = b; // copy assignment operator

-N
 
G

Guest

Just need to clarify the order at which assignement takes place

class A
{
public:
A(){ std::cout << "default ctor"; }
A(const A& a){ std::cout << "copy ctor"; }
A& operator= (const A& a){ std::cout << "assignment operator"; }
~A(){}

};


int main(int argc, char* argv[])
{
A a ; // default ctor
A b(a) ; // copy ctor
A c = b ; // default ctor THEN assignemnt OR copy ctor ???

Copy constructor, assignment will only occur if the object assigned to
already exists, so add this fourth case to you code to get an assignment:

a = c; // Assignment

BTW: Do not forget to add "return *this;" in the assignment operator.
 
E

Eric Johnson

OT:
I am using the clunky MSVC IDE, so am unable to quickly compile and test
this (without creating a project in the IDE) - incidentally, if anyone
knows how to compile litle code snippets like this using a command line
compiler that runs on Windoze (possibly the VC complier?) - without
needing invoking the IDE/ requiring a project etc, ala gcc - please post
instructions etc, so in future I can carry out such tests myself )

I would recommend getting to know MSVC a little better. It's actually
not very difficult to create a little test program like this.
Just create a new "console application" and in the advanced setting
make it an "empty project". Much faster than posting to a group like
this. Plus, you get the full use of the debugger.
 
J

James Kanze

Just need to clarify the order at which assignement takes place
class A
{
public:
A(){ std::cout << "default ctor"; }
A(const A& a){ std::cout << "copy ctor"; }
A& operator= (const A& a){ std::cout << "assignment operator"; }
~A(){}
};
int main(int argc, char* argv[])
{
A a ; // default ctor
A b(a) ; // copy ctor
A c = b ; // default ctor THEN assignemnt OR copy ctor ???

There's no assignment here. In this statement, the = is not an
operator, but simply punctuation. The formal semantics are to
convert the initializer expression to the target type, then copy
constructor. (If the initializer expression already has the
target type, this is exactly the same as the precedant. If it
doesn't, e.g. A has a constructor A::A(int), and you write:
A b( 42 ) ;
A c = 42 ;
the first doesn't require a copy constructor, the second does.)
OT:
I am using the clunky MSVC IDE, so am unable to quickly compile and test
this (without creating a project in the IDE) - incidentally, if anyone
knows how to compile litle code snippets like this using a command line
compiler that runs on Windoze (possibly the VC complier?) - without
needing invoking the IDE/ requiring a project etc, ala gcc - please post
instructions etc, so in future I can carry out such tests myself )

The command is cl---but of course the corresponding directory
has to be in your path. By default, however, some of the
options necessary to use the standard libraries are turned off;
you'll probably want at least "/vmg /GR /EHs
/D_CRT_SECURE_NO_DEPRECATE". I'm also not too sure about how it
links by default; I always specify /D__DEBUG /MTd (although I'm
not sure why:)).

If you invoke "cl /help", you'll get a fairly detailed list of
the options. There are probably some others that might be
interesting. Put them all in a shell variable, and invoke the
compiler with them, e.g. "cl $VCPPFLAGS ...".

(I'll admit that I don't understand the why of the defaults. I
know of no compiler where the defaults are usable for actual
development, but VC++ is the only one I know where you need
options to compile things like "Hello, world!".)
 
J

James Kanze

Just need to clarify the order at which assignement takes place
class A
{
public:
A(){ std::cout << "default ctor"; }
A(const A& a){ std::cout << "copy ctor"; }
A& operator= (const A& a){ std::cout << "assignment operator"; }
~A(){}
};
int main(int argc, char* argv[])
{
A a ; // default ctor
A b(a) ; // copy ctor
A c = b ; // default ctor THEN assignemnt OR copy ctor ???

There's no assignment here. In this statement, the = is not an
operator, but simply punctuation. The formal semantics are to
convert the initializer expression to the target type, then copy
constructor. (If the initializer expression already has the
target type, this is exactly the same as the precedant. If it
doesn't, e.g. A has a constructor A::A(int), and you write:
A b( 42 ) ;
A c = 42 ;
the first doesn't require a copy constructor, the second does.)
OT:
I am using the clunky MSVC IDE, so am unable to quickly compile and test
this (without creating a project in the IDE) - incidentally, if anyone
knows how to compile litle code snippets like this using a command line
compiler that runs on Windoze (possibly the VC complier?) - without
needing invoking the IDE/ requiring a project etc, ala gcc - please post
instructions etc, so in future I can carry out such tests myself )

The command is cl---but of course the corresponding directory
has to be in your path. By default, however, some of the
options necessary to use the standard libraries are turned off;
you'll probably want at least "/vmg /GR /EHs
/D_CRT_SECURE_NO_DEPRECATE". I'm also not too sure about how it
links by default; I always specify /D__DEBUG /MTd (although I'm
not sure why:)).

If you invoke "cl /help", you'll get a fairly detailed list of
the options. There are probably some others that might be
interesting. Put them all in a shell variable, and invoke the
compiler with them, e.g. "cl $VCPPFLAGS ...".

(I'll admit that I don't understand the why of the defaults. I
know of no compiler where the defaults are usable for actual
development, but VC++ is the only one I know where you need
options to compile things like "Hello, world!".)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top