I was wondering, can I and if I do, how to call a copy constructor of template class A in copy constructor of class B, both class are not in the same file.
template<typename T> class A: public AP<T>
{
public:
A();
A(const A&);
~A();
};
#include "A"
class B: public BP
{
private:
A<int> Amember;
public:
B();
B(const B&);
~B();
};
Thanks :)
If I understand the question, I think you're after the optional initializer list in the constructor of your B class. Doesn't matter if you want to call the default constructor or copy constructor. In the definition of the constructor, you can put a colon and the list of how each member variable should be initialized. i.e., which constructor should be called.
#include <iostream>
using namespace std;
class A{
public:
A(){cout<<__PRETTY_FUNCTION__<<endl;}
A(int i){cout<<__PRETTY_FUNCTION__<<" "<<i<<endl; data = i;}
A(const A& a){cout<<__PRETTY_FUNCTION__<<endl; data = a.data;}
private:
int data;
};
class B{
public:
B() : a(){}
B(int i) : a(i){}
B(const B& b) : a(b.a) {}
private:
A a;
};
int main(){
B b1; // default constructor
B b2(5); // integer constructor
B b3(b2); // copy constructor
}
Related
Suppose I have class A
class A{
public:
A(const A&) {};
A() {};
~A() {};
bool bln;
B b;
}
If I write
A *a2;
a2 = new A(*a);
where a is an A object, then a2->b seems different to a->b.
How can I exactly copy a to a2?
bln is not being initialized in any of A's constructors. b is only being default constructed in both constructors, and is not being copied at all in the copy constructor.
Try this:
class A
{
public:
bool bln;
B b;
A() : bln(false) {}
A(const A &src) : bln(src.bln), b(src.b) {}
};
A better option is to simply let the compiler generate a default copy constructor for you, which will effectively be the same as above:
class A
{
public:
bool bln;
B b;
A() : bln(false) {}
};
Or, in C++11 and later, you can do use:
class A
{
public:
bool bln = false;
B b;
// these are optional in this case and can be omitted
A() = default;
A(const A&) = default;
};
Let's say we have two classes in C++:
Class A{
public:
A();
private:
int k;
};
Class B{
public:
B();
private:
A a;
};
I edit my question such that it is more helpful for anyone does reach it someday.
How could I write the copy ctor of B (is it a copy ctor indeed?) for initializing a (which is of type Class A) with another object instance of A (let it be a_inst) which have already been defined and initialized before?
In other words, what would be the code for the ctor B()?
I would do it like this
B(const A ¶mA) : a(paramA) {}
B(A &¶mA) : a(move(paramA)) {}
B(const B &src) : a(src.a) {}
Beside the copy constructor I am also proposing two additional constructors, one that can be initialized by l-value instances of A and other for r-value instances. Depending on the semantics of A you may not need the move version.
Class A{
public:
A();
A(const A& obj);
private:
int k;
};
Class B{
public:
B();
B(const B& obj)
: a(obj.a) { }
B(const A& obj)
: a(obj) { }
private:
A a;
};
Here is the code to demonstrate the classic problem
class A
{
public:
A(int){}
};
class B
{
vector<A> va; //Error no default constructor available
public:
B(vector<A>v):va(v)
{}
};
The error is no default constructor available. I don't need default constructor for class A, so don't write it.
Adding a trivial constructor for A will work for you
class A
{
public:
A(int) {}
A() {}
};
In the below code I have defined an explicit copy constructor in the derived class. I have also written my own copy constructor in the base class.
Primer says that the derived copy constructor must call the base one explicitly and that inherited members should be copied by the base class copy constructor only, but I copied the inherited members in the derived class copy constructor and it is working fine. How is this possible?
Also how do I explicitly call the base class copy constructor inside the derived class copy constructor? Primer says base(object), but I am confused how the syntax differentiates between normal constructor call and copy constructor call.
Thanks in advance.
#include<stdafx.h>
#include<iostream>
using namespace std;
class A
{
public:
int a;
A()
{
a = 7;
}
A(int m): a(m)
{
}
};
class B : public A
{
public:
int b;
B()
{
b = 9;
}
B(int m, int n): A(m), b(n)
{
}
B(B& x)
{
a = x.a;
b = x.b;
}
void show()
{
cout << a << "\t" << b << endl;
}
};
int main()
{
B x;
x = B(50, 100);
B y(x);
y.show();
return 0;
}
copy constructor is when you have another Object passed to your constructor:
class A() {
private:
int a;
public:
//this is an empty constructor (or default constructor)
A() : a(0) {};
//this is a constructor with parameters
A(const int& anotherInt) : a(anotherInt) {};
//this is a copy constructor
A(const A& anotherObj) : a(anotherObj.a) {};
}
for a derived class
class B : public A {
private:
int b;
public:
//this is the default constructor
B() : A(), b() {};
//equivalent to this one
B() {};
//this is a constructor with parameters
// note that A is initialized through the class A.
B(const int& pa, const int& pb) : A(pa), b(pb) {}
//for the copy constructor
B(const B& ob) : A(ob), b(ob.b) {}
}
How somebody else wrote here:
How to call base class copy constructor from a derived class copy constructor?
I would write the copy constructor like this instead how it was written by macmac:
B(const B& x) : A(x) , b(x.b)
{
}
To call the copy constructor of the base A you simply call it passing the derived B object A(B), is not neccessary to specify B.a.
EDIT: macmac edited his answere in the correct way, now his answere is better then mine.
I have a nested class structure, Class A is parent and Class B is nested under.
When I compile the code, the copy constructor of Class A reports that there is no default constructor for the class B.
error: no default constructor exists for class "A::B"
class A{
-------
struct B{
B(var1, var2){}
};
B b;
};
A::A(){ b = new B(Var1, Var2) } // default constructor
A::A(a){ } // copy constructor
Any ideas on how to fix this ?
You need to use a member initializer in both constructors:
A::A() : b(Var1, Var2) {}
A::A(const A& a) : b(Var1, Var2) {}
You defined a constructor taking two arguments B(var1, var2) as such a default constructor is not automatically provided for you.
So you have a couple of options.
Choice 1
Add a default constructor for b in your struct B definition
struct B{
....
B() {};
}
also your syntax below is wrong it should be:
A::A() : b() {};
A::A( const A& a) : b() {};
Choice 2
You could use the non default constructor of B but you have to come up with values from somewhere
A::A() : B( valA, valB ) {};
A::A( const A& a) : b(valA, valB) {};
Choice 3
You probably don't want that and instead should create a copy constructor for B and do this
A::A( const A& a) : b(a.b) {};