You are not logged in.

#1 2010-08-20 02:27:09

RetroX
Member
Registered: 2009-10-17
Posts: 106

[solved] C++ - Inherited Classes

#include <iostream>

struct base
  {
  void func1() { std::cout << "base::func1" << std::endl;
  void func2() { func1(); }
  };

struct inherited : public base
  {
  void func1() { std::cout << "inherited::func1" << std::endl; }
  };

This is probably a pretty stupid question, but I have absolutely no clue how to do this.  I'm trying to get inherited::func2 to call inherited::func1 instead of base::func1 without defining inherited::func1.  This really is a conceptualised version of what I'm trying to accomplish, but it explains it well enough.

I'm using the latest version of the GCC with the experimental C++0x support.

Last edited by RetroX (2010-08-21 16:08:13)

Offline

#2 2010-08-20 02:47:39

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [solved] C++ - Inherited Classes

You've inherited it, so its part of the class.

int main(void) {
  inherited Foo;
  Foo.func2();
  return 0;
}

Offline

#3 2010-08-20 02:56:51

RetroX
Member
Registered: 2009-10-17
Posts: 106

Re: [solved] C++ - Inherited Classes

I tried that, and the output was base::func1.

Really, what I'm trying to do is avoid repeating code.  But it seems likely that I'll have to at this point.

Last edited by RetroX (2010-08-20 02:59:41)

Offline

#4 2010-08-20 03:01:26

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [solved] C++ - Inherited Classes

Ahhh, I see what you're trying to do. Someone more familiar with C++ than I might be able to tell you how to muck with vtables to get inherited.func1() called instead of base.func1() (if its even possible).

Last edited by falconindy (2010-08-20 03:02:24)

Offline

#5 2010-08-20 03:12:59

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: [solved] C++ - Inherited Classes

#include <iostream>

struct base
  {
  virtual void func1() { std::cout << "base::func1" << std::endl;
  void func2() { func1(); }
  };

struct inherited : public base
  {
  virtual void func1() { std::cout << "inherited::func1" << std::endl; }
  };

Last edited by PirateJonno (2010-08-20 07:09:06)


"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page

Offline

#6 2010-08-20 03:16:35

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,487
Website

Re: [solved] C++ - Inherited Classes

You only need the virtual in the base struct, not the inherited.

Offline

#7 2010-08-20 03:25:15

RetroX
Member
Registered: 2009-10-17
Posts: 106

Re: [solved] C++ - Inherited Classes

Yeah, I seem to have gotten it to work.  For whatever reason, I thought that I already tried that, but it didn't work.

Either way, thanks!

Offline

#8 2010-08-20 07:08:37

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: [solved] C++ - Inherited Classes

You're welcome.
@Allan: True, I just prefer being explicit about it.


"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page

Offline

#9 2010-08-21 14:21:43

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [solved] C++ - Inherited Classes

Plese don't use "C/C++".  Your question was about C++ and nothing to do with C.

Offline

#10 2010-08-21 16:08:37

RetroX
Member
Registered: 2009-10-17
Posts: 106

Re: [solved] C++ - Inherited Classes

Trent wrote:

Plese don't use "C/C++".  Your question was about C++ and nothing to do with C.

I've never personally used C, so, I didn't know if it applied to C or not.  Sorry about that.

Offline

Board footer

Powered by FluxBB