How to construct/declare this kind of recursivity ?

Hello cplusplus forum (first post)

I need some help to construct or declare this kind of recursivity:

I have 2 classes A and B.
Both, A and B, have a set of similar methods that are coded in class C.
And some C methods calls A/B methods, example:

Class A {
m1;
m2 that calls m3 in class C;
}

Class B {
m1;
m2 that calls m3 in class C;
}

Class C {
m3;
m4 that calls m1 in class A or B;
}

Some times I will create an object from class A and after that I will call A.m4 and other times I will create an object from class B and will call B.m4

Each class is in a different file (I will appreciate some help on how to setup the headers in this case).

I'm kind of C++ beginner, that includes class-based programming beginner.

Thank you
Marcio
Last edited on
hahha... omg I can't believe how often this is coming up now.

I just wrote an article on this very topic.

http://www.cplusplus.com/forum/articles/10627/

Skip to section 4 if you already know the basics.
Thank you (for both, answer here and write the wiki). I will read it, test and get back.
Still need some help to get it. For example, no pointers, references, ...

//------------------------------- main program
#include "B.h"
int main()
{
B objB;
objB.m_B();
return 0;
}


//------------------------------ Class B header
#ifndef B_H_
#define B_H_
class B
{
public:
void m_B();
};
#endif


//---------------------------- Class B implementation
#include "B.h"
void B::m_B()
{
m_A();
}

//--------------------------- Class A header
#ifndef A_H_
#define A_H_
class A
{
public:
void m_A();
};
#endif

//------------------------------- Class A implementation
#include <iostream>
#include "A.h"

void A::m_A()
{
std::cout << "Hi";
}



g++ -O3 -Wall -c -fmessage-length=0 -oB.o ..\B.cpp
..\B.cpp: In member function `void B::m_B()':
..\B.cpp:11: error: `m_A' was not declared in this scope
Build error occurred, build is stopped
You seem to have a fundamental misunderstanding of how classes work.

You can't call a member function in class A without having an instance of class A. Member functions are not like global functions -- they require an instance of the class (IE: and object) on which to perform their function. You cannot call a [non-static] member function without provided that object.

EDIT

to clarify:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class A
{
public:
  void m_A() { std::cout << "m_A called\n"; }
  void m_A2()
  {
    m_A();  // works because it's a fellow member function.
        // it perform the operation on object 'this' (ie:  the object
        //  currently being operated on in this function)
  }
};

int main()
{
  m_A();  // doesn't work (what object are you calling m_A on?)

  A myA;  // first, make an object of the class
  myA.m_A();  // then call it -- this works

  return 0;
}
Last edited on
I see. Maybe you could help me understand with the program example below. The output of that "main program" is:

<Start1><Common><Finish1>
<Start2><Common><Finish2>

common() is really a common code and should be coded in a single place.
Because I was not able to do that, I had to code it twice, once inside class B and another inside class C.

I was thinking that a 3rd class ( class A ) could be the solution. common() should be there.

Notice, that start() must call common() and common must call finish().
start() and finish() are both different for B and C.

How can I do that ?

Thank you
Marcio



//------------------------------------------- main program
#include "B.h"
#include "C.h"
int main() {B obj1; obj1.start(); C obj2; obj2.start(); return 0;}

//------------------------------------------- class B header
class B {public: void start(); void common(); void finish();};

//------------------------------------------- class B implementation
#include <iostream>
#include "B.h"
void B::start(){std::cout << "<Start1>"; common();}
void B::common(){std::cout << "<Common>"; finish();}
void B::finish(){std::cout << "<Finish1>" << std::endl;}

//------------------------------------------- class C header
class C {public: void start(); void common(); void finish();};

//------------------------------------------- class C implementation
#include <iostream>
#include "C.h"
void C::start(){std::cout << "<Start2>"; common();}
void C::common(){std::cout << "<Common>";finish();}
void C::finish(){std::cout << "<Finish2>" <<std::endl;}
Last edited on
Topic archived. No new replies allowed.