I wrote this code to learn the shared_ptr object creation inside my own class.
Why is A object shared_ptr destroyed even before A's print (obj->print() inside B's print) is called?
#include <iostream>
#include <memory>
using namespace std;
class A {
private: string str;
public:
A(string s){
str = s;
cout<<"class A: "<< str << endl;}
~A() { cout << "destroying A: " << str << endl;}
void print() { cout << "A print called" << endl;}
};
class B {
private: shared_ptr<A> obj;
public:
B() { cout << "default construct called" << endl;}
B(string s) {
cout << "shared_ptr creation" <<endl;
shared_ptr<A> obj = make_shared<A>(s);
cout << "shared_ptr creation done" <<endl;
}
~B() { cout<<"destroying B" << endl;}
void print() { obj->print();cout << "B print called" << endl;}
};
void func( B i) { i.print();}
int main()
{
{
B b("hello world");
func(b);
}
cout << "in main" << endl;
return 0;
}
output:
shared_ptr creation
class A: hello world
shared_ptr creation done
destroying A: hello world
A print called
B print called
destroying B
destroying B
in main
In B::B(string), shared_ptr<A> obj = make_shared<A>(s); is constructing a local object which gets destroyed immediately when the constructor ends; it has nothing to do with the data member obj, which points to nothing as the result.
I think you want:
B(string s) {
cout << "shared_ptr creation" <<endl;
obj = make_shared<A>(s); // assignment to the data member obj
cout << "shared_ptr creation done" <<endl;
}
Or initialize it in member initializer list.
B(string s) : obj(make_shared<A>(s)) { // initialize the data member obj
cout << "shared_ptr creation done" <<endl;
}
Related
The following class outputs when a constructor is called:
class A {
public:
A() {
std::cout << "Default Constructor called at address:" << this << "!\n";
}
A(int val) : val_(val) {
std::cout << "Secondary Constructor called at address:" << this << "\n";
}
A(A&& other) : val_(std::move(other.val_)) {
std::cout << "Move constructor called from " << &other << " to " << this << "\n";
}
A& operator=(A&& other) {
std::cout << "Move assignment called from " << &other << " to " << this << "\n";
val_ = std::move(other.val_);
return *this;
}
private:
int val_;
};
This union allows me to skip the default constructor of A, which is desirable:
union MyUnion {
MyUnion() {}
A a;
};
My goal with main was to move-construct a. Instead it move-assigns the a.
int main(){
MyUnion my_union[2];
std::cout << "Entering for loop:\n";
for (int i = 0; i < 2; ++i){
my_union[i].a = std::move(A(i));
}
return 0;
}
An example output from the above main function is:
Entering for loop:
Secondary Constructor called at address:0x7ffcdd3553a8
Move assignment called from 0x7ffcdd3553a8 to 0x7ffcdd3553b0
Secondary Constructor called at address:0x7ffcdd3553a8
Move assignment called from 0x7ffcdd3553a8 to 0x7ffcdd3553b4
I wanted to move-construct, but I'm instead move-assigning. Move-assigning without move constructing is unsafe in general. The solution I came up with was to use std::construct_at. I also added a destructor. The full program with these modifications is below:
#include <iostream>
#include <memory>
#include <utility>
class A {
public:
A() {
std::cout << "Default Constructor called at address:" << this << "!\n";
}
A(int val) : val_(val) {
std::cout << "Secondary Constructor called at address:" << this << "\n";
}
A(A&& other) : val_(std::move(other.val_)) {
std::cout << "Move constructor called from " << &other << " to " << this << "\n";
}
A& operator=(A&& other) {
std::cout << "Move assignment called from " << &other << " to " << this << "\n";
val_ = std::move(other.val_);
return *this;
}
~A() {
std::cout << "Destructor called at " << this << "\n";
}
private:
int val_;
};
union MyUnion {
MyUnion() {
std::cout << "MyUnion Constructor called!\n";
}
void ConstructAt(A&& other){
std::construct_at(&a, std::forward<A>(other));
}
A a;
~MyUnion(){
std::cout << "MyUnion Destructor called!\n";
a.~A();
}
};
int main(){
MyUnion my_union[2];
std::cout << "\nEntering for loop:\n";
for (int i = 0; i < 2; ++i){
my_union[i].ConstructAt(std::move(A(i)));
}
std::cout << "\nExiting for loop:\n";
return 0;
}
This gives output:
MyUnion Constructor called!
MyUnion Constructor called!
Entering for loop:
Secondary Constructor called at address:0x7ffe38dd4838
Move constructor called from 0x7ffe38dd4838 to 0x7ffe38dd4840
Destructor called at 0x7ffe38dd4838
Secondary Constructor called at address:0x7ffe38dd4838
Move constructor called from 0x7ffe38dd4838 to 0x7ffe38dd4844
Destructor called at 0x7ffe38dd4838
Exiting for loop:
MyUnion Destructor called!
Destructor called at 0x7ffe38dd4844
MyUnion Destructor called!
Destructor called at 0x7ffe38dd4840
This appears to be what I want.
Question
Was this the correct way to call the move constructor? Am I using std::forward correctly? Is this the correct way to handle the destructor of a union?
Having some trouble with passing instances of objects around in other objects. In the end of the main application I want iDogClassOne and iDogClassTwo to reference the same object and I want that object to contain the numbers that were placed in variableA. What am I doing wrong here? The addresses of tempDogClass, iDogClassOne, and iDOgClassTwo are all different so I am pretty sure I am currently creating 3 instances of the dog class which I don't want to do..... Any help would be greatly appreciated.
class dogClass{
public:
void setVariableA(std::vector<double> newNum) {
variableA.push_back(newNum);
}
void printVariableA() {
if (variableA.size() != 0) {
std::cout << variableA[0][0] << std::endl;
std::cout << variableA[0][1] << std::endl;
}
}
dogClass &dogClass::operator=(const dogClass &src) {
variableA = src.variableA;
return *this;
}
private:
std::vector<std::vector<double>> variableA;
};
class animalClass{
public:
void addNumberToDog(std::vector<double> num) {
dogClass tempDogClass;
getDogOne(tempDogClass);
tempDogClass.setVariableA(num);
std::cout << "Address of temp Dog: " << &tempDogClass << std::endl;
std::cout << "Variables in tempDog:\n";
tempDogClass.printVariableA();
std::cout << std::endl;
}
void getDogOne(dogClass &dogOne) {
dogOne = dogs[0];
}
void setDogOne(dogClass &dogOne) {
dogs.push_back(dogOne);
}
private:
std::vector<dogClass> dogs;
};
int main() {
animalClass iAnimalClass;
dogClass iDogClassOne;
iAnimalClass.setDogOne(iDogClassOne);
std::vector<double> newNum;
newNum.push_back(12);
newNum.push_back(15);
iAnimalClass.addNumberToDog(newNum);
dogClass iDogClassTwo;
iAnimalClass.getDogOne(iDogClassTwo);
//Why are these not equal. Also why does dog Two not contain the values in dog One?
std::cout << "Address of dog One: " << &iDogClassOne << std::endl;
std::cout << "Variables in dog One:\n";
iDogClassOne.printVariableA();
std::cout << std::endl;
std::cout << "Address of dog Two: " << &iDogClassTwo << std::endl;
std::cout << "Variables in dog Two:\n";
iDogClassTwo.printVariableA();
std::cout << std::endl;
return 0;
}
output of console application (why does iDogClassOne and iDogClassTwo not contain the numbers? also why are tempDog, iDogClassOne, and iDogClassTwo not the same object?)
Address of temp Dog: 00000000002AF8A0
Variables in tempDog:
12
15
Address of dog One: 00000000002AF938
Variables in dog One:
Address of dog Two: 00000000002AF920
Variables in dog Two:
Because you are making copies of the dogClass objects in many places.
Please think about the difference of values, references and pointers.
For example in the main() function you call iAnimalClass.setDogOne(iDogClassOne); The argument is passed as reference, that is ok. But then you copy it into the vector. This is little bit tricky because you cannot pass the reference in a vector. In this case you will have to use a vector of pointers to dogClass.
Now you already have two instances of dogClass. One in the main function, another one in the vector of the animalClass.
The next problem is in the main() function where you instantiate iDogClassTwo.
Now you have three instances of dogClass.
Then you call iAnimalClass.getDogOne(iDogClassTwo); which copies the dog from the vector to the dog in the main function.
Here is the corrected Version:
#include <vector>
#include <iostream>
class dogClass{
public:
void setVariableA(std::vector<double> newNum) {
variableA.push_back(newNum);
}
void printVariableA() {
if (variableA.size() != 0) {
std::cout << variableA[0][0] << std::endl;
std::cout << variableA[0][1] << std::endl;
}
}
dogClass & operator=(const dogClass &src) {
variableA = src.variableA;
return *this;
}
private:
std::vector<std::vector<double>> variableA;
};
class animalClass{
public:
void addNumberToDog(std::vector<double> num) {
dogClass& tempDogClass = getDogOne();
tempDogClass.setVariableA(num);
std::cout << "Address of temp Dog: " << &tempDogClass << std::endl;
std::cout << "Variables in tempDog:\n";
tempDogClass.printVariableA();
std::cout << std::endl;
}
dogClass& getDogOne() {
return *dogs[0];
}
void setDogOne(dogClass &dogOne) {
dogs.push_back(&dogOne);
}
private:
std::vector<dogClass*> dogs;
};
int main() {
animalClass iAnimalClass;
dogClass iDogClassOne;
iAnimalClass.setDogOne(iDogClassOne);
std::vector<double> newNum;
newNum.push_back(12);
newNum.push_back(15);
iAnimalClass.addNumberToDog(newNum);
dogClass& iDogClassTwo = iAnimalClass.getDogOne();
//Why are these not equal. Also why does dog Two not contain the values in dog One?
std::cout << "Address of dog One: " << &iDogClassOne << std::endl;
std::cout << "Variables in dog One:\n";
iDogClassOne.printVariableA();
std::cout << std::endl;
std::cout << "Address of dog Two: " << &iDogClassTwo << std::endl;
std::cout << "Variables in dog Two:\n";
iDogClassTwo.printVariableA();
std::cout << std::endl;
return 0;
}
why does iDogClassOne and iDogClassTwo not contain the numbers?
int main()
{
animalClass iAnimalClass;
dogClass iDogClassOne;
iAnimalClass.setDogOne(iDogClassOne); // pushes a copy of iDogClassOne
// into a vector animalClass::dogs
std::vector<double> newNum;
newNum.push_back(12);
newNum.push_back(15);
iAnimalClass.addNumberToDog(newNum); // Only operates on function local variable
// variable tempDogClass without having
// any effect on the outside world.
dogClass iDogClassTwo;
iAnimalClass.getDogOne(iDogClassTwo); // copies the copy of iDogClassOne earlier pushed
// into the vector animalClass::dogs to iDogClassTwo
std::cout << "Address of dog One: " << &iDogClassOne << std::endl;
std::cout << "Variables in dog One:\n";
iDogClassOne.printVariableA();
std::cout << "\nAddress of dog Two: " << &iDogClassTwo << std::endl;
std::cout << "Variables in dog Two:\n";
iDogClassTwo.printVariableA();
}
why are tempDog, iDogClassOne, and iDogClassTwo not the same object?
Why are a, b and c not the same object in following example?
void func()
{
int c;
}
int main()
{
int a;
int b;
func();
}
Consider the following C++ code:
#include <iostream>
using std::cout;
class A
{
public:
int a;
A():a(0)
{
cout << "A constructor\n";
}
virtual void f()
{
cout << "f inside A\n";
}
};
class C : public A
{
public:
int c;
virtual void f()
{
cout << "f inside C\n";
}
C():c(0)
{
cout << "C constructor\n";
}
};
int main()
{
A varA = C();
cout << "Size of C class: " << sizeof(C) << "\n";
cout << "Size of varA object: " << sizeof(varA) << "\n";
C* varC = static_cast<C*>(&varA);
varC->f();
cout << "varC->a is " << varC->a << "\n";
cout << "varC->c is " << varC->c << "\n";
}
The output of this program is:
A constructor
C constructor
Size of C class: 16
Size of varA object: 8
f inside A
varC->a is 0
varC->c is 1726166356
I initialize the varA object with the constructor of class C. Constructors of A and C class are called, but the varA is a simply a A object. I cast the address of varA to C* type and I try to call its f() function, but it prints the f() function of class A, so I deduce that it is use the early binding mechanism to call it.
I think if I call the constructor of derived class, like this case, I obtain the same object if I had called the base constructor.
I think the only difference is the other constructors are called. Is my supposition right or there are any other differences?
Classic example of slicing. A varA = C(); leaves you with object of static and dynamic type of A. As a result, C* varC = static_cast<C*>(&varA); exhibits undefined behavior.
You can store a complete derived class in a base-class pointer, however:
int main() {
A* varA = new C();
C* varC = static_cast<C*>(varA);
varC->f();
cout << "varC->a is " << varC->a << endl;
cout << "varC->b is " << varC->b << endl;
cout << "varC->c is " << varC->c << endl;
} // oops, forgot to delete varA/varC, memory leak!
I want to know know how does g++ compiler knows which table to use if their are multiple vtable present in a base class. Like the following example.
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
class sample1
{
private:
int b;
public:
sample1():b(34)
{
cout << "In sample1 constructor" << endl;
}
virtual void print_b()
{
cout << this->b << endl;
}
void print_all()
{
this->print_b();
}
void sample_print_()
{
//cout << this->a << "String : " << this->str1 << endl;
cout << "hello" << endl;
this->print_all();
}
};
class sample2
{
private:
int b1;
public:
sample2():b1(34)
{
cout << "In sample1 constructor" << endl;
}
virtual void print_b1()
{
cout << this->b1 << endl;
}
void print_all1()
{
this->print_b1();
}
};
class sample : public sample1 , public sample2
{
private:
int a;
char *str1;
public:
sample():a(12),sample1()
{
strcpy(this->str1,"hello world");
cout << "In Constructor" << endl;
}
~sample()
{
free(this->str1);
cout << "In Destructor" << endl;
}
void sample_print()
{
//cout << this->a << "String : " << this->str1 << endl;
cout << "hello" << endl;
this->print_all();
}
virtual void print_a()
{
cout << this->a <<endl;
}
};
In above example, child class sample has two parent classes sample1 and sample2 and each of these class have vtable of their own. What if i call a virtual function from sample(child class)? How does the compiler know, in which class that virtual function is present so that it call use that particular vtable pointer ? I know their will be two vtable pointer present in sample(child class) class , so how does compiler know which one to use ?
I have an explicit function that takes a reference to the base type of a class. What is the proper way to pass that in?
I am currently doing a static cast:
#include <iostream>
using namespace std;
struct Base
{
Base() { cout << "Base Constructor" << endl; }
Base(Base const& c) { cout << "Base-Base Constructor" << endl; }
};
struct Derived : public Base
{
Derived() { cout << "Derived Constructor" << endl; }
explicit Derived(Base const& c) { cout << "Derived-Base Constructor" << endl; }
Derived(Derived const& c) { cout << "Derived-Derived Constructor" << endl; }
};
int main()
{
Base B;
cout << "\n";
Derived D;
cout << "\n";
Base* test1 = new Derived(D);
cout << "\n";
Base* test3 = new Derived(static_cast<Base>(D));
cout << "\n";
Base* test2 = new Derived(B);
cout << "\n";
return 0;
}
but that calls the copy constructor of the base class.
I could pass *static_cast<Base*>(&D), but that seems a bit hackish. I feel like I am just overlooking a simple way to do this. Thanks.
Use this:
static_cast<Base&>(D)
Or this:
static_cast<const Base&>(D)