LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2006, 07:11 AM   #1
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Rep: Reputation: 35
Inheritance question


I have:
Code:
class A {
};
Code:
class B: public A {
};
Code:
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.
 
Old 08-23-2006, 07:33 AM   #2
cupubboy
Member
 
Registered: May 2003
Location: Bucharest,Romania
Distribution: Fedora Core 7
Posts: 109

Rep: Reputation: 15
I'm not sure I understand
why don't you override it??? .. Declare and define a D::Update .. not that's a very good desing .. but ..
 
Old 08-23-2006, 07:41 AM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
yeh, override the update() function in D..

in C, make Update() virtual

then in D, define an Update()
 
Old 08-23-2006, 08:36 AM   #4
kornerr
Member
 
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Original Poster
Rep: Reputation: 35
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;
}
 
Old 08-23-2006, 09:55 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.
 
Old 08-23-2006, 11:25 PM   #6
sarutobi
LQ Newbie
 
Registered: Aug 2006
Posts: 1

Rep: Reputation: 0
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();
	}
}
Can anybody write C++ code from my Java template?
 
Old 08-24-2006, 07:38 AM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> But I didn't understand why upcast isn't work in C++.

you cant cast from an A to a B, a B to an A sure, but not an A to a B..

edit> oops. sorry i didnt see in D where you say a = new B(), i take back my previous statement, your cast should work..

Last edited by xhi; 08-24-2006 at 07:40 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
enum inheritance in C++? graemef Programming 2 01-27-2006 10:14 PM
Template inheritance allomeen Programming 7 01-26-2006 03:08 PM
C++ and inheritance question niteshadw Programming 7 08-08-2005 04:43 PM
inheritance kalleanka Programming 4 02-29-2004 07:19 PM
OOP / Python inheritance question usernamenumber Programming 1 12-02-2003 10:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration