Class Board_type{ //This is "Board_type.cpp" file
public :
Board_type(void);
~Board_type();
};
Typedef struct ctxdata_t{ // This is "ctxdata.c" file
Board_type *my_board;
}ctxdata;
void Main(){
ctxdata *data;
// Getting error in the below line
(data->my_board)->Board_type(); //calling constructor of Boad_type through the structure ctxdata_t.
}
Error : ctxdata.c : calling type `Board_type' like a method.
But why are you calling the constructor. it will be called automatically. When you create any class object than the constructor/destructor's are called automatically.
The issue here is we are allocating memory by using our own built-in function.
we are not allocating memory using "new" operator.
instead of new we are using our propritary built-in function.our function behaves as same like new but added functionality..
So your function must be calling the constructor internally for you. So you don't have call the constructor.
Just create the object and use the methods of the class. Read documentation of your proprietary function. You are calling the constructor and that's why your compiler is giving you the error.
Than how does it make objects for other classes? There may be something then. If something is written in the constructor how will that execute if the function is not calling the constructor.
Here, we are using malloc function to allocate a memory for the class. Malloc takes the size as the input argument, it doesn't bother about either its class or structure, simply allocates memory. In the above case, the constructor will not be called. This is exactly our problem. So when we are allocating memory for class using malloc, we have to call the constructor explicitly right?
we are calling the constructor using the class pointer for which the memory is allocated using mallioc, is raising a compiler error when calling like as follows.
(data->my_board)->Board_type();
Error : ctxdata.c : calling type `Board_type' like a method.
Is there any possiblity to call the constructor using class pointer???