Call child method from superclass

Hi,

I want call the method "getImage" of child class from the superclass.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
## SplashScene.cpp

void SplashScene::init()
{
    cout << std::string image = getImage(); << endl;
}

std::string SplashScene::getImage() // this is a virtual method in header
{
    return "splash1.png";
}

## SplashScene1.cpp // it extends SplashScene

std::string SplashScene1::getImage() // implement override method
{
    return "splash5.png";
}


When i try use:

1
2
SplashScene1 *s1 = new SplashScene1();
s1->init();  // it return splash1 


What is wrong! :(
Last edited on
What are your child class and superclass?
You can call child method using a pointter or a reference of superclass that points or references the child object. For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

struct SuperClass
{
   virtual void show() { std::cout << "SuperClass\n"; }
   virtual ~SuperClass() {}
};

struct Child
{
   virtual void show() { std::cout << "Child\n"; }
};

int main()
{
   SuperClass *obj = new Child;

   obj->show();
}
closed account (zb0S216C)
In addition to vlad's post, you can choose which show() function you want. This is achieved with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct BaseClass
{
    virtual ~BaseClass() { }
    virtual void Function() { std::cout << "Base::Function()\n"; }
};

struct ChildClass : public BaseClass
{
    virtual void Function() { std::cout << "Child::Function()\n"; }
};

int main()
{
    ChildClass NewChildClass;

    NewChildClass.BaseClass::Function();
    NewChildClass.Function();

}

Wazzak
The problem is that i have a class SplashManager that receive a list of SplashScene and i have 3 classes that inherits from SplashScene, these are:

SplashScene1, SplashScene2, SplashScene3

And the manager call:

SplashScene1->init();
SplashScene2->init();
SplashScene3->init();

The problem is when i call "init". Instead of child classes call owner init method, its calling the super class method "SplashScene::init".

The code of SplashScene1 is:

# header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef SplashScene1_H
#define SplashScene1_H

#include "SplashScene.h"

class SplashScene1 : public SplashScene
{
public:
    SplashScene1();
    virtual ~SplashScene1() {}
    virtual void init();
private:
    virtual std::string getImage();
};

#endif 


# implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "SplashScene1.h"

SplashScene1::SplashScene1()
{
    SplashScene::SplashScene();
}

void SplashScene1::init()
{
    SplashScene::init();
}

std::string SplashScene1::getImage()
{
    return "splashDevGames.png";
}
I solve the problem.

The init method need be called from child class:

1
2
3
4
SplashScene1::SplashScene1()
{
    init();
}


Instead of call super class to do it.
Although it's a good idea to initialize your object in the constructor, that is not the issue.
But you are not showing your code, cout << std::string image = getImage(); << endl; it does not compile.

You are trying to do the 'template pattern'. Now, ¿why is init() virtual? ¿and why do you override it to do the exact same thing that was doing in the base class?

1
2
3
4
SplashScene1::SplashScene1()
{
    SplashScene::SplashScene(); //c++ ain't java. You are constructing a base object here
} // that dies here 
To call an specific base constructor (or member constructor), you need to use an initialization list derived::derived(): base(42){
Last edited on
> The init method need be called from child class:
> Instead of call super class to do it.

The point is that init() is a virtual function, and you are calling it from the constructor.

http://www2.research.att.com/~bs/bs_faq2.html#vcall
http://www.artima.com/cppsource/nevercall.html

Topic archived. No new replies allowed.