A class function that uses an instance of its child class as argument

I am facing a real-life problem, it can be simplified as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

class B;

class A
{
    public:
        void f1(A a) {}
        void f2(B b) {}
};


class B : public A
{
};


int main()
{
    A a;
    B b;
}


There is no problem at all with the f1(), it compiles and executes without any problem.
But f2() gives compilation error. How to solve this?

The error message is:

error: 'b' has incomplete type


This is just to define the function f2() in a class, that uses an instance of its child class as one of its arguments.
Please help, thanks in advance.
Last edited on

How about this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

// forward declares:
class B;

class A
{
public:
	A() {}
	virtual ~A() {}
	void f1(A* a) {}
	void f2(B* b) {}
};

class B : public A
{
public:
	B():A() {}
	virtual ~B() {}
};

int main()
{
    A a;
    B b;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

class B;

class A
{
    public:
        void f1(A a) {}
        void f2(B b);
};

class B : public A
{
};

inline void A::f2( B b ) {}

int main()
{
    A a;
    B b;
}
@ajh32


You declared different functions compared to functions in the original post. Also there is no need to specify explicitly the constructor of B and to call explicitly the constructor of A in the mem-intilaizer list of B().
Last edited on
@vlad from moscow

Just because I pass the argument by a pointer doesn't mean that its wrong. It's also good practice to declare a default constructor and to call it's base class constructor. Why don't you tell me why not?
The default constructor for B will automatically call the default constructor for A, unless you specify different behaviour. There's no need to write it out in constructor for B - it's just code clutter.
@ajh32
Just because I pass the argument by a pointer doesn't mean that its wrong. It's also good practice to declare a default constructor and to call it's base class constructor. Why don't you tell me why not?


I did not say that your functions incorrect. I said that they are different functions. And I do not know about your "good practice" of declaring a default constructor and calling a base default constructor when there is no such a need. At least it would be much better if you would define the default constructor of class A as

A() = default;
Last edited on
At least it would be much better if you would define the default constructor of class A as

A() = default;

Isn't that a C++11 construct? Most people won't be using that yet.
@MikeyBoy

Isn't that a C++11 construct? Most people won't be using that yet.


Most people do not use what they do not know.:) And as it is known most people know little in C++.:)
True... but also, a lot of people reading this will be working in an environment where they can't use C++11, so I thought it was worth mentioning :)
Dear ajh32, vlad and Mikey Boy,

So far there are two options to solve it.
1). using a class pointer instead as the argument
2). declare the class function first, then define it outside the class definition.

Both works, thanks.
The problem is solved.

Thank you very much.
Last edited on
You're welcome!
Topic archived. No new replies allowed.