Clarification needed on Copy constructors topic

Firstly, thanks for the great site here. Without it i would
have not come this far in becoming familiar and feeling my way
round C++.

I am currently trying to grasp what constructors do in general
from the normal "constructor" to the "copy constructor".
This is where i have found myself at this page :
http://www.cplusplus.com/articles/y8hv0pDG/

Now, i do not want to be picky or anything but my brain is
having a little hard time comprehending this part. Maybe it
can be changed/updated a little..

Quote:
****
Note that none of the following constructors, despite the fact that
they could do the same thing as a copy constructor, are copy
constructors:
1
2
  MyClass( MyClass* other );
  MyClass( const MyClass* other );

****
Hence, excuse the dumb question to ask then but ARE they actually
copy constructors in a round about way?

To try and contribute, i have playing around with constructors,
and found that declaring a constructor like below, that assigns a value
to the integer in the constructor, still builds the object in the main function.

1
2
3
4
5
6
7
8
9
10
class World1
{
public:
    World1 (int id=9)
...
int main ()
{
	//World1 myWorld0(1) ;
	World1 myWorld ;
}


Thanks again,

M.
Hence, excuse the dumb question to ask then but ARE they actually
copy constructors in a round about way?


They may be constructors that initialise their object by copying from another object of the same type, but that does not make them copy constructors.

"Copy constructor" is a term that very specifically means a constructor that takes, as its first argument, a reference to an object of the same type; typically:

A(const A&);

I have no idea what you're trying to say in the second part of your post. Your constructor takes an int, and you've specified a default value for that int. There's nothing unusual about that, and it has nothing to do with copy constructors. Could you clarify what you're trying to say, please?
Thanks Mikey much appreciated in helping out! :o)

The second part is just related to constructors in general..

To clarify the last part, let me paste the full code. then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class World1
{
public:
    World1 (int id=9)
		  : _identifier (id)
    {
        std::cout << "Hello from " << _identifier << ".\n";
    }

    ~World1 ()
    {
        std::cout << "Good bye from " << _identifier << ".\n";
    }
private:
    const int _identifier;
};

//World1 TheWorld ;

int main ()
{
	//World1 myWorld0(1) ;
	World1 myWorld ;


    /*
    for (int i = 3; i < 6; ++i)
    {
        World aWorld (i);
    }*/
   // World oneMoreWorld (6);
}


It seems you have already answered my question when you
mentioned the magic words "default value"..

So, if a default value is given when defining your constructor,
for example "World1 (int id=9)" instead of "World1 (int id)"
then you do not need to define them in your main function.
So again i see constructors behave similar to normal functions then..
So, for clarity and completeness, if i do define a constructor
WITHOUT a default value hence:"World1 (int id)", then i MUST add a int after my object in main like "World1 myworld(99)"..

I got the above code from the following site that really helps in
explaining scope :http://www.relisoft.com/book/lang/scopes/1global.html


A default value for a parameter can be given for any method or function, not just for a constructor, e.g.

1
2
3
4
5
6
7
8
9
10
class A
{
public:
  // ...
  void myMethod(int a = 0, float b = 1.23, bool c = true);
  // ...
};

myMethod();   // equivalent to myMethod(0, 1.23, true);
myMethod(4, 5.67);   // equivalent to myMethod(4, 5.67, true); 


Your C++ textbook should explain all this in detail, so there's no point me repeating it here.
Last edited on
Ok, thanks again..


//M.
Semantics is fun. For the first, consider:
1
2
3
4
MyClass *foo = 0;
MyClass bar( foo );
MyClass * pint = new MyClass[42];
MyClass pub( pint );

The bar cannot be a copy and pub does not know how many pints were offered. Way too uncertain. Thus, a copy constructor is better to be one that makes a copy from a verified source. A pedantic would read the standard and then interrogate its authors.


Your World1 has a non-default constructor, which takes one integer parameter. That parameter has a default value. Therefore, your compiler can and will replace the World1 myWorld; with World1 myWorld( 9 );. That is not an "assignment", nor "in constructor".
Topic archived. No new replies allowed.