Disallowing inherited classes

D

daniel.w.gelder

Hello, I would like to write a function that accepts a reference to an
instance of a particular class, but will not accept instances of any
subclasses. For example:

struct A {};
struct B : A {};
inline void MyFunction(A& a) {}

A a;
B b;
MyFunction(a); // great!
MyFunction(b); // syntax error!

Is there a cheap way to do this?

Thanks.
Dan
 
P

Phlip

daniel.w.gelder said:
Hello, I would like to write a function that accepts a reference to an
instance of a particular class, but will not accept instances of any
subclasses. For example:

struct A {};
struct B : A {};
inline void MyFunction(A& a) {}

A a;
B b;
MyFunction(a); // great!
MyFunction(b); // syntax error!

Is there a cheap way to do this?

A sternly worded comment.

The entire point of OO is clients of derived things have no need to
distinguish them from base things. Look up the Liskov Substitution
Principle.

If you don't need to derive, you don't need to inherit, so you shouldn't
worry about folks in the future inheriting.

You might get what you need with an assertion and 'typeid'.

If they find a reason to inherit, and if they pass the "wrong type" into
your function, then they will treat whatever assertion you use as a
nuisance, and will erase it or work around it.
 
D

daniel.w.gelder

Oh yeah?

struct A {}
struct B : A {}
struct C : A {}

A* a;
B* b;
C* c;

b = dynamic_cast<B*>(a); // compiler would accept...an A* might be a
B*
b = dynamic_cast<B*>(c); // compiler would reject...a C* can't be a B*

Same code except you're passing a C* instead of an A*.
 
C

codigo

Hello, I would like to write a function that accepts a reference to an
instance of a particular class, but will not accept instances of any
subclasses. For example:

struct A {};
struct B : A {};
inline void MyFunction(A& a) {}

A a;
B b;
MyFunction(a); // great!
MyFunction(b); // syntax error!

Is there a cheap way to do this?

Yes, exactly what Philip said.

struct B { };

struct A
{
B b; // composition
public:
A() : b() { }
~A() { }
};

inline void MyFunction(A& a) {}

A a;
B b;
MyFunction(a); // great!
MyFunction(b); // syntax error!
 
C

codigo


Reread Philip's comment. A car is not an engine. A car has_an engine.

class Car
{
Engine e;
};

so...

inline void MyFunction(Car& car) {}

Car car;
Engine engine;
MyFunction(car); // great!
MyFunction(engine); // syntax error!

Just cause i can jump off a bridge doesn't mean i should...
 
T

Terry

Oh yeah?

struct A {}
struct B : A {}
struct C : A {}

A* a;
B* b;
C* c;

b = dynamic_cast<B*>(a); // compiler would accept...an A* might be a
B*
b = dynamic_cast<B*>(c); // compiler would reject...a C* can't be a B*

Same code except you're passing a C* instead of an A*.

If you are intent on writing unreadable and unmaintainable code.
Please carry on. There is plenty out there.

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top