Jump to content
  • Advertisement
Sign in to follow this  
starstriker1

Calling a parent class?

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm wondering if its possible for a class to reference a function from the class it inherited from. Specifically, I've got functions in a class that are exactly the same as the ones from the parent class, except with a tiny bit of code tacked on the end. Can I access the parent to save typing?

Share this post


Link to post
Share on other sites
Advertisement
In C++, use BaseClass::functionName() to access that function. Of course, if you haven't overloaded it in the derived class, you can just use functionName().

Share this post


Link to post
Share on other sites
Yes...in C++ anyhow:


BaseClass
{
public:
BaseClass(){};
~BaseClass(){};

void aFunc()
{
std::cout << "Hello - BaseClass here" << std::endl;
}
}

DerivedClass : public BaseClass
{
public:
DerivedClass(){};
~DerivedClass(){};

void aFunc()
{
BaseClass::aFunc();
std::cout << "and I'm a DerivedClass" << std::endl;
}
}



I think that's right..I haven't done that for ages..:)

EDIT: Too slow this time;)

Share this post


Link to post
Share on other sites
To call the base-class constructor from the derived class constructor, use the initializer list:


class A
{
public:
A(int i) { }
};

class B: public A
{
public:
B(float j) : A(3) { }
};


Share this post


Link to post
Share on other sites
in C++ ... like this:

// basic declaractions
class Base
{
public:
Base(int i);

void NonVirt();
virtual void Virt();
};

class Derived : public Base
{
public:
Derived(int x, int y);

void NonVirt();
virtual void Virt();
};

// somewhere in Derived.cpp

Derived::Derived(int x, int y) : Base(x+y) // this is calling the base constructor - its called an "initializer list"
{
// do stuff special here
}

void Derived::NonVirt()
{
// special stuff before
Base::NonVirt();
// special stuff after
}

void Derived::Virt()
{
// special stuff before
Base::Virt();
// special stuff after
}

Share this post


Link to post
Share on other sites
yes you can. Let me make sure that you are asking what I think you are.

You have a function defined in a base class and you have a different version in subclass. Your want the subclass version of the function to essentially extend the base class version of the function. Ok I'm not completely sure about the exact answer so I'm going over to my bookshelf. I have a wonderful book called "The C++ Programming Language" by Bjarne Stroustrup, the creator of C++. I very strongly suggest that you buy yourself a copy.

Just as I thought, here is how you do it (for the full example buy the book!): Manager is a sublcass of Employee

void Manager::print() const
{
Employee::print(); //calls the base class version
cout<<"\tlevel " << level << '\n';
}

Share this post


Link to post
Share on other sites
Quote:
Original post by starstriker1
One question: is accessing a parent class as such considered bad form? Are there any pitfalls to watch for?

Nah, not really.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!