ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
class C {
public:
void Update ();
protected:
A *a;
};
Code:
void C::Update () {
a->Check ();
}
And:
Code:
class D: public C {
protected:
B *a;
};
I want to be able to call D::Update () so that B *a is updated, not A *a.
Is there any way to accomplish this?
And I don't want to use C::Update (A* a) which will defenitely work if I pass B* a to it.
Thanks.
Ok, this is kinda ugly, but it does what I want. Overriding is what I escape since I want to have one base class that contains common functions so that I can use them from derived classes.
Code:
#include <iostream>
using namespace std;
class A {
public:
void AFunc () {
cout << "AFunc\n";
}
};
class B: public A {
public:
void BFunc () {
cout << "BFunc\n";
}
};
class C {
public:
C () {
a = new A ();
}
void Update () {
a->AFunc ();
}
protected:
A *a;
};
class D: public C {
public:
D () {
b = new B ();
a = b;
}
protected:
B *b;
};
int main () {
D d;
d.Update ();
return 0;
}
Hmm thats nasty code, heres a few points:
Any class which is a base for another class should have a virtual destructor.
You create a D which calls the constructor C which creates a pointer. D then creates a new pointer and assigns it to the pointer created by C; Memory leak. There are two copies of the pointer in two classes which is responsible for the destruction? Neither clean up memory leak.
Hmm... I wrote some Java code that solve problem. But I didn't understand why upcast isn't work in C++.
Code:
public class A {
public A() {
super();
msg = "A";
}
protected String msg;
public String get(){
return msg;
}
}
public class B extends A{
public B() {
super();
msg = "B";
}
public String test(){
return "Test";
}
}
public class C {
protected A a;
public C() {
super();
a = new A();
}
public void Update(){
System.out.println(a.get());
}
}
public class D extends C{
public D() {
super();
a = new B();
}
public void Update(){
super.Update();
System.out.println(((B)a).test()); //<- This is upcast a to B.
}
}
public static void main(String[] args){
C c = new C();
c.Update();
D d = new D();
d.Update();
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.