Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

C++ interface imitation


  • Please log in to reply

#1
W-Unit

W-Unit

    Member

  • Member
  • PipPipPip
  • 170 posts
Hey guys,

So, having come from a background of Java as my first language, and being of the opinion that C# is a nearly-perfect OOPL, I am frequently annoyed with C++'s lack of support for interfaces.
I often will "solve" this by making use of multiple inheritance, but recently I had a situation arise that got me thinking....

Let's say I have these classes:
struct Interface { virtual char foo() const = 0; };
struct Base: public Interface
{
	int z;
	bool blah() const;
	virtual char foo() const override;
};
And now I want to create a class Derived. If I make Derived inherit from Base, then it also inherits from Interface. But what if I want different access rules for the members inherited from Base and those inherited from Interface? Say I want Derived::blah() and Derived::z to have protected access, while Derived::foo() has public access.
Virtual inheritance, I think, would probably work for this... however, what if I must also be able to downcast data of type Interface* to Derived*? Virtual inheritance makes this type of casting impossible (except with reinterpret_cast, but that is probably the nastiest curse word in the C++ vocabulary that I know of)

Is it then acceptable to declare Derived like so?
struct Derived: protected Base, public Interface { ... };
This means that each Derived object will have two Interface sub-objects, but is that really an issue, seeing as the Interface class consists only of function members, with no data?

Thanks!! :)

Edited by W-Unit, 25 August 2011 - 02:55 PM.

  • 0

Advertisements


#2
W-Unit

W-Unit

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 170 posts
Also, is there a way to check if a downcast is legal?
For instance...
struct A { };
struct B: public A { };
struct C: public A { };

int main()
{
	A* ptA1 = new C();
	A* ptA2 = new B();

	B* ptB1 = static_cast<B*>(ptA1);	// Should NOT be possible, but this compiles anyway
	B* ptB2 = static_cast<B*>(ptA2);	// Works as it should
	B* ptB3 = dynamic_cast<B*>(ptA2);	// Will not compile, even though this is legal

	return 0;	
}
The main thing I am interested in here is the illegal cast that occurs in ptB1. Even though this cast converts between two non-related types, it still returns a valid pointer, and we could still try to access and modify ptB1 data, which leads to unpredictable behavior and (likely) garbage information.
Is there a way I can check at runtime whether such a downcast is valid?

Thanks! :)
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP