Interfaces , Concrete Classes and Inheritance

this is a little drawn out but i have two interfaces IWireframe and IPointMeasureable

Then i have Wireframe that implements IWireframe and then three classes point, line and plane that Inherit Wireframe.

Only Point Implements IPointMeasureable but I'm finding when i cast to IPointMeasureable on either Line or Plane it does not come back as NULL im confused why this is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma once

class IWireframe
{
	virtual void SetUuid()=0;
};


#pragma once

class IPointMeasureable
{
	
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once

#include <iostream>
#include "IWireframe.h"

class Wireframe : public IWireframe
{
private:
	const char* Uuid;
	virtual void SetUuid() override;
protected:
	Wireframe();
	~Wireframe();
public:
	bool Activity = true;
	bool Hidden = false;
	char* Name;
	char* Color = "White";
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once
#include "IPointMeasureable.h"
#include "Wireframe.h"

class Point : public  Wireframe , public IPointMeasureable
{
private:
	double m_X = 0;
	double m_Y = 0;
	double m_Z = 0;
private:
	friend class GeometryFactory;
	Point();
	Point(double X, double Y, double Z);
	~Point();
};


1
2
3
4
5
6
7
8
9
10
#pragma once
#include "Wireframe.h"

class Line : public  Wireframe
{
private:
	friend class GeometryFactory;
	Line();
	~Line();
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
#include "Point.h"
#include "Line.h"
#include "Plane.h"

class GeometryFactory
{
public:
	GeometryFactory();
	Point* CreatePoint();
	Point* CreatePoint(double X, double Y, double Z);
	Line* CreateLine();
	Plane* CreatePlane();
	~GeometryFactory();
};


1
2
3
4
5
6
7
8
9
10
11
12
13
//Works Fine as it should
IPointMeasureable* PointMeasurableInterface = (IPointMeasureable*)NewPoint;
	if (NULL == PointMeasurableInterface)
	{
		iLog.Error("No Interface for IPointMeasureable");
	}

//Still able to cast to an interface that is not implemented on the Line class.
	IPointMeasureable* PointMeasurableInterface2 = (IPointMeasureable*)NewLine;
	if (NULL == PointMeasurableInterface2)
	{
		iLog.Error("No Interface for IPointMeasureable");
	}
Last edited on
C-style casts never give a null pointer for a valid pointer, but if the source type and the destination type are unrelated, the final pointer may be invalid.

The cast you want is static_cast:
1
2
3
4
5
6
7
8
9
10
class A{};
class B{};
class C{};

class D : public A, public B{};

int main(){
    D d;
    C *c = static_cast<C *>(&d); //Compiler error
}
Note that static cast does not return a nullptr. It simply makes the compiler reject the program if the types are unrelated, since there's no possible execution where the cast would be valid.
interesting, thank you. There does not seem to be a clear way in which to determine all the interfaces that have been implemented on an object. Do you know of anyway to list all interfaces that have been implemented?

Cheers

1
2
3
	IPointMeasureable IP;
	Point* p = static_cast<Point*>(&IP);
	Line* l = static_cast<Line*>(&IP); //invalid type conversion 
Last edited on
No, C++ has no concept of "interface" in the OO sense. All it sees are classes inheriting from other classes. There's no way to list all the classes that an object is an instance of.
There is a way to check if an object is an instance of a specific type, though:
1
2
3
4
5
6
7
8
9
10
11
class A{};

class B : public A{};
class C : public A{};

int main(){
    A *a = new B;
    C *c = dynamic_cast<C *>(a);
    assert(c == nullptr);
    C &c2 = dynamic_cast<C &>(*a); //exception thrown
}
dynamic_cast performs the same checks that static_cast performs, but also checks at run time that the objects are of the correct type. For example:
1
2
3
4
5
6
int main(){
    A *a = new B;
    C *c = static_cast<C *>(a);
    assert(c != nullptr);
    C &c2 = static_cast<C &>(*a); //NO exception thrown
}
The c pointer is not null and no exception was thrown, but the pointers are invalid because they don't point to valid instances of C.

Note that having to use dynamic_cast often indicates a poor OO design. Also, dynamic_cast has a hefty run time cost in some implementations. You should avoid using it in performance-sensitive code.
thank you.

so i did have a thought its programmer dependent

in my concrete class that inherits from the interface i added

 
std::list<std::string> InterfaceList;


then in the constructors i just add the interface string to the list that way they stack up as my class structure becomes more complex and i can always add a HasInterface method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once

#include <iostream>
#include <list>
#include <string>
#include "IWireframe.h"

class Wireframe : public IWireframe
{
private:
	const char* Uuid;
	virtual void SetUuid() override;
protected:
	Wireframe();
	~Wireframe();
public:
	std::list<std::string> InterfaceList;
	bool Activity = true;
	bool Hidden = false;
	char* Name;
	char* Color = "White";
};
Last edited on
Topic archived. No new replies allowed.