Is there a way to embed terralang into a C++ application, such that C++ classes can be extended from terra, including overloading virtual functions?
BONUS: Can this be done (semi?)automatically for a (large)set of classes, without needing to write "wrapper code" for each class and each virtual method?
Related
I thought interface can only include pure virtual functions in C++. However, recently my college showed me the following code:
class IAddition {
public:
virtual bool MethodA() = 0;
static bool StaticMethodA(IAddition* interface) {
return interface->MethodA();
}
static std::string GetStr() {
return "A";
}
};
The interface can be compiled but I feel the code weird. I can't understand what is a static method of an interface? Is this very normal in C++ programs?
I thought interface can only include pure virtual functions in C++
There is no such requirement imposed by the C++ standard.
Is this very normal in C++ programs?
Yes(assuming by normal you mean well-formed), this is a valid C++ program. We're allowed to use virtual member function with static as well as non-static member functions within a class.
As blankettripod point out in comment and Jason Liam in other answear C++ language do not have interface concept. So purely from C++ standard perspective this is fine.
But in many best C++ practices interface means:
class or a struct without any fields and only with public pure virtual functions inside (except for destructor which must be implemented and should be virtual).
See also Cpp Core Guidelines I.25: Prefer empty abstract classes as interfaces to class hierarchies.
So your IAddition fails this definition because of this static methods.
Many developers will not have problem with that.
On other hand in many projects where coding standard rules are more strict and respected, this is no longer an interface and should be fixed. There are many rationales, for example if this API is ported to other languages (like Java or C# where interfaces are part of language features) this extra methods will be a real technical problem.
I need to link my c-program with functions from inherited c++ library. The standard way is to use c++ wrappers to define a dummy c-structure that represents the c++ class. That works for simple (not inherited) classes. How can I modify it to work with inherited classes (i.e. air->cloud->lightning)?
Simple case:
typedef struct lightning lightning;
Can an interface in C++ have non virtual functions ?
or
An interface in C++ can contain only non virtual function (proto types)?
Generally an "interface" class (in just about all OO languages) is a class with just an interface, no data and no implementation.
In C++ such a class is a class with only pure abstract functions, so no you can't have an "interface" class in C++ if you have non-virtual functions.
C++ does not have interfaces per se. Thus no restrictions are imposed on them.
Abstract classes are usually used to represent interfaces. It is a matter of convention whether you define default implementations for virtual methods and allow final methods in interface-like classes.
This question already has answers here:
How do you declare an interface in C++?
(17 answers)
Closed 8 years ago.
I wanted to write shell extensions for windows in plain C++, but then I got confused by the keyword interface. In many articles I read that I can create interfaces in C++ by writing classes containing only virtual methods without any code. For example:
class IIsThisAnInterface_QuestionMark {
virtual MyMethod (
int firstParameter,
double secondParameter) = 0;
virtual AnotherMethod (
wchar_t *firstParameter) = 0;
}
But the author of this article defined interfaces by using the interface keyword. So my question is: How to correctly define interfaces in C++? (Becuase I grew up in C#'s world, I know interfaces as constructs specifying methods for classes that are implementing these interfaces.)
C++ doesn't strictly provide interfaces in the way that some languages do. The C++ mechanism is to provide a class with one or more pure virtual methods that declare the desired interface. Strictly speaking such a class is just an abstract class, but one could consider calling it an interface that child classes would then implement.
I am getting a bit confused how to call C++ functions in Objective C.
I've declared my functions as:
void InitializeSearchRegistration();
In the header file in between the interface and end. I've then defined it in my mm file as:
void InitializeServiceRegistration()
{
}
What I am struggling with is how to define it as public to ensure other classes can see it. I've tried public but get an error. Usually C++ methods have to be declared in a class block.
Any advice would be great.
You can't add C++ member functions to an Objective-C class using Objective-C++. You should consider redesigning your class to either have a totally C++ interface (in which case it should be a C++ class or struct) or a totally Objective-C interface (in which case it should define ObjC class and instance methods, not C++ member functions).
There are two ways to go about this, the first is to combine C++ and Objective C into another language, called Objective C++. The other is to communicate over plain C functions.