Shared Pointer for parent and child [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a problem with the initialization of some classes. Simplified code looks like:
class Base
{
Base(int)
};
class BaseChild : public Base
{
};
class mainWindow
{
boost::shared_ptr<Base> pBase;
void init();
};
void mainWindow::init()
{
this->pBase = boost::shared_ptr<Base>(new Base(12));
this->pBase = boost::shared_ptr<Base>(new BaseChild(12));
}
So the problem is with initialization of BaseChild class which is child from Base class. What am I doing wrong? I thought that parentral class pointer can point on child class.
Generaly my program has to work in such way:
When it starts, there is initialization of parental class (in above example: this->pBase = boost::shared_ptr<Base>(new Base(12));). This already works.
In some case, when some flag change its value, pointer which point on parental class object should be change to point on child class object.

You miss a constructor that takes int in BaseChild. Also constructor in Base is private, which makes it unusable outside Base class.
Try
class Base
{
public:
Base(int);
};
class BaseChild : public Base
{
public:
BaseChild(int);
};

Related

C++ How does order of instance creation matter? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a class that creates instances of other classes, and when I call them, compiler gives me warning about order of the instances. Why does it matter? It does same job, regardless of the order.
E.g. I have this in my core class header file (core class handles game loop):
HUD hud;
World myWorld;
Like this they do all they need to. But compiler gives a warning:
'Core::myWorld' will be initialized after [-Wreorder]|
Then if I put myWorld instance above the hud instance, it doesn't give me warning anymore. I was just wondering, how on earth does it matter which order they are in?
Warning is since, in constructor initializer-list you initialize World before HUD, but actually members will be initialized in order they are declared in class.
Just litle example, where it can be worse:
class B
{
public:
B(int i) : value(i) {}
private:
int value;
};
class A
{
public:
A() : value(10), b(value)
{
}
private:
B b;
int value;
};
Here b will be initialized before value and so, something will be sended to b constructor, but not 10 as programmer want.
With
struct C
{
C() : p(std::make_unique<int>(42)), j(*p) {} // initialization doesn't use this order.
// but the order here:
int j;
std::unique_ptr<int> p;
};
you will dereference nullptr as j is initialized before p.

C++ Inheriting base Class Destructor [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm working on a game, where i have a derived player class that inherites from a Parent GameObject Class, What i want to accomplish is calling the base class destructor inside of the derived class destructor, can i do that?
Example:
// Base Class
class A
{
public:
// other code goes here...
~A();
protected:
int a;
}
// ...
// ...
// Base Class Destructor
A::~A()
{
// sets a back to 0
a = 0;
}
// Derived Class
class B : public A
{
public:
// other code goes here...
~B();
}
// Derived Class Methods
B::~B()
{
// Calls for Base Class Destructor, How can i accomplish this
A::~A();
}
Parent class' destructor is automatically called. Calling order of destructors is opposite to order of constructors; so that, it's ok to rely on parent class' fields in destructor of derived class.
You should better declare destructor as virtual. It's needed to determine correct destructor if you delete object of derived class through base class pointer.
Try adding trace output in destructors to ensure what calling order of destructors is.

How to call a function in all inheritant classes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
For example let's say I have this class:
class base{
public:
update();
};
and I have a lot of classes that are inherit from this one:
class A : public base
{
//<...>
}
class B : public base
{
//<...>
}
and so i want to know - can i somehow call the function base::update(); in all the classes without writing it all out (A::update(); B::update();) etc.
Like calling one function that would do the update(); in all the classes that inherit it from the base class.
Thanks!
Edit:
What I'm doing is changing my entity system into component based one and I want to know whether there's an easier way to call the (let's say) update(); function, that is inherited, in all the members. Instead of doing enemy01.update(); enemy02.update(); etc. to just write down a single update(); that'd work on all classes that have it(inherited it), sorta like a message to all of them to call the function.
A virtual function?
class base{
public:
virtual void update();
};
And then this function:
void update_class(base &b) {
return b.update();
}
You can call update_class on any class that inherits base.

Virtual function, how does they work? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
In C++, i am not able to understand, when a base class pointer stores the address of derived class object it is not able to call the derived class member function?
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show()
{
cout<<" In Base ";
}
};
class Derived: public Base
{
public:
int x;
void show()
{
cout<<"In Derived ";
}
Derived()
{
x = 10;
}
};
int main(void)
{
Base *bp, b;
Derived d;
bp = &d;
bp->show();
cout << bp->x;
return 0;
}
According to me:
derived d => allocates the memory to this object(therefore to x also ) say at address 200,
bp = &d; => it allocated the address 200 to bp. Now it should be able to call bp->x?
But it gives a error.
bp->x gives an error because bp is a pointer to an object of type Base, and Base doesn't have a variable called x, only Derived does.
If x was moved up into Base it would be accessible from both Base and Derived if it's public or protected.
Binding of names happens in run time. so at compilation bp is of type base. so compiler doesn't know anything about derived type assignment. so its saying there is no variable called x in base.
That's where concept of virtual functions come into picture. but hey are only for functions not variables.
In C++, i am not able to understand, when a base class pointer stores
the address of derived class object it is not able to call the derived
class member variable?
Well yes you can, you just need to cast the pointer to the class where the member variable is present
cout << dynamic_cast<Derived*>(bp)->x;
Why is bp->x is a error?
Because at compile time, the compiler can't tell what it points to. What if it was actually, a pointer to the base class? Then, x would be completely absent from the underlying object...
To answer the title question: when you have a pointer to base class that actually points to a derived class instance, then a call to a public functions declared virtual in the based class and reimplemented in the derived class will end up being a call to the derived class' implementation of that function (there are subtelties involved, with private/public access and name hiding that may interfere, but this is roughly how it works).

Pass Object to Class via Constructor [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Why does this code not work:
int main () {
Bob bob;
bob.giveANewFredSomeData();
Fred::sharedFred.getANumber(); //Crashes here due to someData inside fred being NULL
return 0;
}
Class Data {
int aNumber;
}
Class Bob {
void giveANewFredSomeData{
Data data;
Fred::sharedFred = new Fred(data);
}
}
Class Fred {
Data someData;
static sharedFred;
Fred (Data data) {
someData = data;
}
int getANumber(){
return someData.aNumber
}
}
Your code is not working because:
You need a semicolon after the declaration of a class
static is not a valid type
static objects have to be initialized outside of their definition in the class in order to be used elsewhere in the code. You need something like: Fred Fred::sharedFred; before main
Declaration of a function must have () in front of the function name before the {} braces
the classes have to be in the scope of main function for them to be used, and also in the scope of each other depending on what is calling what. i.e. main has to be declared after the class and same for each class that calls another
Properties/methods declared in a class are private by default. To make group of properties/methods public, have the keyword public followed by a colon at the top of the group