C++ syntax. Is this a copy concstructor?

I'm not as familiar with C++ as I am with other languages like C#.

In the following code, on line 6, are stacks s2,s3,s4,s5 being defined and initialized as copies of s1? The class for Stack has no constructor that takes one parameter. (I think C# would complain that there was no constructor defined that took one parameter.)
int _tmain(int argc, _TCHAR* argv[])
{
  Stack s1;
  for (int i = 1; i < 5; i++)
    s1.push(i);
  Stack s2(s1), s3(s1), s4(s1), s5(s1);
.
.
.
class Stack
{
    int items[10];
    int sp;
  public:
    friend class StackIter;
    Stack()
    {
        sp =  - 1;
    }
.
.
.

Open in new window

deleydSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
It is called "implicitly defined copy constructor". C++ compiler creates one for you if you have not created one. It works as a member-wise copy (I believe in C# it is called deep copy, or member wise copy.
Read about it here
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
to add to before comment:

the member-wise copy is only a deep copy, if it doesn't contain (C) pointers or reference types:

class X;
class Y;

class A
{
     char * psz;
     X* px;     
     Y& y;
public: 
     A() psz(new char[10]), px(NULL), y(*new Y()) {} 
};

Open in new window


any of the 3 members would prevent from a deep copy. instead the pointers and references would be copied what means that a copy of an object A would have the same pointers (values) and same reference as the original object (what is called a flat copy).

in this case you have to add an own copy constructor (and assign operator=):

A(const A& a) : psz(new char[10]), px(NULL), y(*new Y(a.y)) 
{ 
    strcpy(psz, a.psz); 
    if (a.px)
    {
         px = new X(*a.px);
    }
}  

Open in new window


Sara
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.