How do i declare a class constructor in IDL? - c++

How is a class constructor define in IDL?

You don't. IDL is about interfaces, not how objects are constructed. Clients don't need to know those details, just how to interact with such an object when it's available on the network.

Constructor is used to create and initialize an object. However, a constructor creates the object locally, that is, within the address space of the process that calls the constructor. Because of this, a constructor cannot be used to create an object in a different process, and this is the reason why you cannot define a constructor for an IDL interface.
If you want to create an object in a different (server) process, you can use the factory pattern, which invokes an operation on an existing object in the server. E.g.,
interface ObjectFactory {
Object create(...);
...
};

Related

C++ preventing user from creating object instances

I have to create simple singleton object factory for some types of objects. The problem is that I can't find smart way to prevent user from creating object instances by constructors. I know that I can move constructors to private/protected section but how will the factory create new objects now when constructors are private/protected? Making factory a friend of every class isn't really smart as I need to predeclare factory in every header and write aditional "friend Factory;" in every class. How to do this correct?
In the class, add a static member as pointer to the same type of the class. This will be your singleton.
When initializing an instance, the constructor is ran.
If this is the first time, the static member is null (never initialized), run the constructor normally and set default values. In the end also set the static member to this. Now your singleton is initialized.
In subsequent constructor calls, the static pointer will not be null. Make a temporary pointer to the class type. Set this pointer to this, then set this to the static member and delete the pointer. This will delete the new instance and return the same static instance instead, everytime.

Update/Init function or constructor (data not available at construction time)

I have a class which stores some data.
The problem is that the data is not available at construction time.
Important: The classes are for an SDK with read-only classes (ONLY getters).
The classes only stores data from an online API.
Should I use a pointer and create (allocate) the object, when the data is available?
That would require a constructor that throws an exception when the data is invalid or the constructor fails at some point.
Or should I define a default constructor for the object and implement a update/init function, that initializes the class members.
This solution would create the problem, that I have a "zombie" object until the update function is called.
The second problem would be that you could forget to call the update/init function -> causes first problem.
According to the CPP Core Guidelines (http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines):
C.41: A constructor should create a fully initialized object
Reason A constructor establishes the invariant for a class. A user of a class
should be able to assume that a constructed object is usable.
Exception If a valid object cannot conveniently be constructed by a
constructor, use a factory function
followed by:
C.42: If a constructor cannot construct a valid object, throw an
exception
Reason Leaving behind an invalid object is asking for
trouble.
.

Passing any object type to a function C++

I am using a library that provides different types of Signal Objects.
The library has a method:
void sendSignal(boost::shared_ptr<ISignal>)
Each Signal object provided by the library implements the ISignal interface. The ISignal interface does not provide any cloning methods. However, all the objects that implement ISignal do have a copy constructor.
I want to create a function that takes any type that implements ISignal, clones that object by calling the copy constructor, and returns the new copied object. I want to call sendSignal multiple times on different copied objects.
The code currently calls the copy constructor of the different objects in multiple locations. I was hoping there is a simple way of perhaps passing template to a function and have that function copy that object for me, by assuming that the object has a copy constructor.
However, my problem is I can't pass ISignal because that doesn't specify any copy constructor, and I can't pass a specific object that implements ISignal .
Is there any way I can do this, without C++11?
template<class XSignal,
class=typename std::enable_if<
std::is_base_of<ISignal,XSignal>::value
>::type
>
std::shared_ptr<XSignal> CopySignal( XSignal const& signal ) {
return std::make_shared<XSignal>(signal);
}
is a function that can accept an instance of any class derived from ISignal and produces a shared pointer copy.
You have to know the actual derived type at compile time to use this function.
A copy constructor of a base class has no means of copying the entire derived object that the base class is a part of. This goes for C++ or C++11. This is fundamental to C++.
The base class must define a virtual clone() method, or something of similar name, and each derived class must override clone() to construct another instance of the derived class on the heap, then return a pointer to it. That is the only portable way to do this in C++.
If the base class has at least one virtual method, you might be able to get away with writing an external implementation of clone() that tries a dynamic_cast to each possible derived class of the base class, and, if it succeeds, implements a derived class-specific object copy.

abstract classes and constructor in C++

I read this question "C++ Abstract Class: constructor yes or no?" and the answers belonging to it.
But according to answers, I understand that we need the constructor to initialize it data members, however I can use its member functions like setter functions in my derived class to initialize the data members, so Why is it important to define a constructor?
The default constructor definition and the member initializations make the class self contained regarding proper setup conditions (valid state).
Usage of setter methods to manipulate the class instance is optional for class clients (including inheriting classes).
You may consider adding more constructor signatures, that the clients can use to inititialze the class members with a single call, and don't require these applying additional setter calls.
It depends on the particular use case, what's more convenient and semantically correct in the end.
Two reasons:
To ensure the objects are always within a valid state.
You need a copy constructor to ensure that data gets copied correctly (e.g., no blind copies of dynamically allocated resources).
Probably more.

Why is Builder pattern better than a Constructor with arguments in the Class's object being created?

Why can we not the different build steps within the constructor itself.
if the build steps take arguments why can't they be provided as arguments to constructor and utilized within constructor to create the object.
AFAIK, in Builder pattern, the client which specific object to create; then what is the advantage in using a builder instead of a Constructor with arguments in the Class's object being created?
Oh! I get it. I was looking at the Wikipedia example and realized why Builder is helpful. It is helpful, when the client does not know which arguments to pass to the constructor as it is very complicated and hence cannot call the constructor directly and get the object. Consequently, he asks for help from the Concrete Builders who know what arguments to pass to constructors and hence get the object created.
Basically, if the client is the one who is mostly going to be passing the arguments to the constructor of the Class whose object is created, then Builder is not that helpful. It is perhaps better to use the prototype. On the other hand, if there is a small finite set of specific objects that can be created from the class by passing arguments to the constructor (or calling setters) to that class and if they are the ones that are frequently used, then it better to encapsulate this argument passing thingy in the Builder class and use them to create the objects for you.