user1344799
user1344799

Reputation: 15

Memory sharing; Inheritance; Base and Derived instances; C++

Ok, this example is pretty straight-forward for the concept I'm trying to understand. I'll just show you the code:

class Base
{
protected:
    string name;
public:
    virtual string getName() const { return this->name; }
    virtual void setName(string name) { this->name = name; }
....
}

class Derived : public Base
{
private:
    double price;
....
}

main(int argc, char** argv)
{

    Base* base = new Base("Base Class");
    Derived* derived = new Derived(base, 453.21);
    derived->setName("Name changed!");

    cout << "Name of instance: " << base->getName() << endl;

    // Desired effect
        Output: 'Name changed!'
    // Actual effect
        Output: 'Base Class'
....
}

The issue for me is this. I want to create an instance of derived class with reference to already created instance of base class, so when I change any member variable of base class trough the derived instance, I can see the change on previously created base instance in the way demonstrated above.

Note: I hope that you will manage to comprehend what I meant, as I am aware that my terminology is probably little off. Please, don't be harsh. :)

Note: I won't be showing / writing constructors, since I am not sure what is the best way to do this, if even any exists and the syntax may be incorrect.

Upvotes: 0

Views: 1073

Answers (2)

masoud
masoud

Reputation: 56479

The way you are trying to do is weird, but you can simply use inheritance concept like this code:

class Base
{
public:
    Base(const string &name) : name(name) {}
    virtual void setName(const string &name) { this->name = name; }
    virtual string getName() const { return name; }
protected:
    string name;
};

class Derived : public Base
{
public:
    Derived(const string &name, double price) : Base(name), price(price) {}
private:
    double price;
};

int main()
{
    Derived* derived = new Derived("Base Class", 453.21);

    derived->setName("Name changed!");

    Base *base = derived;

    cout << "Name of instance: " << base->getName() << endl;
}

Output

Name of instance: Name changed!

You don't need a create Base object and pass it to the derived object.

Instead, create a derived object and pass its address to a Base pointer.

Upvotes: 1

Daniel Frey
Daniel Frey

Reputation: 56863

This seems to indicate the problem:

Base* base = new Base("Base Class");
Derived* derived = new Derived(base, 453.21);

as it is usually not necessary to construct the Base class separatly. Your derived class will already contain a Base instance implicitly, you don't have to add a pointer to one manually and set it from the ctor. I don't know how your ctor looks, but it should look like this:

Derived(const std::string& name, double p) : Base(name), price( p ) {}

If this enough to fix it yourself, good, otherwise post all the code of your example. Instead of the two lines from your code that I quoted above, it should look more like:

Derived* derived = new Derived("Base Class", 453.21);

If you post the code of Derived, it should be obvious for us and it will be much easier to explain it to you on your concrete example.

Upvotes: 1

Related Questions