Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
I declare a generic object. the object type has to do with user input. based on the user input, the object will be declared to one of the existing classes. What is the proper way of declaring the object?
The way I use it now is as follows:
in driver.cpp, I declare the object like this:
BEPOCH cEpoch(diff);

I use it as follows:
C#
cEpoch.p->getinput();
 cEpoch.p->process();
...
where getinput() and process() are defined in all classes with different functionality.
In driver.h, I declare the BEOPCH class as follows:
C#
class BEOPCH
{
  void* p;
  BEPOCH(int diff){
      if(diff == 0) p = (CEPOCH*) malloc(sizeof(CEPOCH));
      else if(diff == 1) p = (DEPOCH*) malloc(sizeof(DEPOCH));
}

In each specific class definition, I make the void pointer point to the class itself:
CEPOCH(void* p){p = this;}

I get the following compilation error:
[BCC32 Error] driver.cpp(576): E2288 Pointer to structure required on left side of -> or ->*


What did I do wrong? What is the right way of doing that?
Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 28-Oct-11 12:47pm    
OK, and where are input and process?
--SA
CPallini 28-Oct-11 13:42pm    
Probably you're trying to do (in a very contrived way and without success) what OOP provides (elegantly) itself. Could you please tell us what are your requirements?

I'm guessing that either CEPOCH isn't a class or it's not defined within the scope of your function.

There isn't any reason to create a void * internally in a class to point to itself. You can always use the 'this' pointer as you're trying to do in your pointer assignment.

If you're trying to pass a point that could be from several different classes, be sure to derive them all from the same class, and then return a pointer to the base class, using the base class type. Virtualize your fucntions in the base class so that derived class functions can be accessed from this base class type.

If it is a class you have other problems with your code. You should not be using 'malloc' to allocate them. Use 'new' instead.
 
Share this answer
 
Bloody gibberish. (http://en.wikipedia.org/wiki/Gibberish[^].)

A pointer does not point to a class, it points to an instance of class (object). If you want to point to CEPOCH, why did you ever make it void*, not CEPOCH*? Having the pointer p to provide access to the same instance is 100% pointless.

Here is the essence of this problem: when you have an instance of the class and a non-static method (also called instance method), it is already passed to it (actually, the same way through the stack exactly as any other parameter. For example:
C++
class MyClass {
public:
   void MyMethod() {
      //...
      this->someField = //... this is a pointer to instance passed as a parameter (implicit)
      //...
   }
   //...
private: //what I explain has nothing to do with access modifiers; this is just to make the example clear
   int someField;
   //...
} //my Class

//...

MyClass myInstance();
myInstance.MyMethod(); //&myInstance (a pointer made of myInstance by getting its address) is passed to MyClass::MyMethod to give you access to the instance

MyClass * myOtherInstance() = new MyClass();
myOtherInstance->MyMethod(); //myInstance (already a pointer) is passed to MyClass::MyMethod
//...
delete myOtherInstance;


In both cases of instance pointers &myInstance and myOtherInstance are passed to method shown on right of "." or "->" and become this pointer. The keyword "this" itself could be omitted in most cases, unless you need to resolve some name conflict; I've shown this "this" syntax just to make the essence of things more clear.

I suggest you start learning object-oriented part of C++ or maybe all of C++ from the very beginning. Right now you're turning to the wrong way. Not to worry, just fix your direction.

—SA
 
Share this answer
 
v6
Comments
Richard MacCutchan 28-Oct-11 13:20pm    
Is there a web site somewhere telling people to use void* for some reason? This is the second time I've seen the misuse of such a thing recently.
Sergey Alexandrovich Kryukov 28-Oct-11 13:41pm    
:-)
Thank you, Richard.
--SA
Espen Harlinn 29-Oct-11 12:51pm    
You do have a point of two, on pointers, in a pointed sort of way - so it's good that you are pointing OP in the right direction :)
Sergey Alexandrovich Kryukov 29-Oct-11 21:30pm    
Your point taken. :-)
Thank you, Espen.
--SA
Probably OOP provides a better way to solve your problem. Have a look, for instance, at Creational Design Patterns at Wikipedia[^].
 
Share this answer
 
Others have answered you correctly. I suspect you are a C programmer without too much experience who is now moving to C++. You have mixed up some C and C++ concepts along with some completely wrong ideas on how to use classes in C++. It would be in your best interest to get a good intro book on C++ and then go through it before you attempt to write actual production code.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Oct-11 15:50pm    
It looks like you are right, my 5.
--SA
Nish Nishant 29-Oct-11 10:10am    
Comment to OP : You have thanked everyione for their input, but sadly you have not considered following any of the advice. You absolutely need to get a good C++ book and learn some fundamentals before you write any more code. [this is in response to your latest answer which a mod may delete soon]
ahshabon 29-Oct-11 11:46am    
Can you please keep this advice to yourself? this does not help!
Sergey Alexandrovich Kryukov 29-Oct-11 21:33pm    
Do you think Nishant need to read the same elementary book he would advise to you? Why would you think so? And, reading a book is the best advice for you; if it cannot help, nothing can help but perhaps an advice to say good by to programming.
--SA
ahshabon 29-Oct-11 21:43pm    
CAN YOU STOP YOUR SILLY COMMENTS? ENOUGH IS ENOUGH!
We're looking at your code, and it makes no sense - no insult intended.

First you are using malloc, which is permissible, but not without using an in-place contructor to initialize the object. Like:
void * fm = malloc(sizeof(Foo)); 
Foo *f = new (fm) Foo();  

The above will use the memory allocated by the call to malloc. new will not allocate any more. You are not however limited to classes. You can use a placement new operator for any type you would allocate with a call to new.

A 'gotcha' for placement new is that you should not release the memory allocated by a call to the placement new operator using the delete keyword. You will have to destroy the object by calling the destructor directly, and then call free to release the memory.
f->~Foo();
free(f);


In your case I would suspect that using new and delete would be a better approach. If you really need to do your own memory management - overload operator new and delete for your classes.

I assume that you actually had T& Epoch as argument to what now reads as int myFunction(T Epoch) and you will need to change that to:
tmemplate<T>
int myFunction(T*& Epoch) 
{
 Epoch = new T(); 
}

My guess is that you are trying to solve what shared_ptr and friends will do for you shared_ptr[^]

But that's just an (educated?) guess ... I've been using C++ for more than 20 years too, and I still read books - books on development, automation and process control, auditing, math, physics, law, security, etc.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
ahshabon 29-Oct-11 13:58pm    
thanks Espen for your input. The code compiles and runs ok now. I, however, use the stack to pass the objects. I had to increase the stack for the code to run without stack overflow. BTW, do you know the downside of using the stack over the heap in this case, if any?
Also, explain what you mean by shared_ptr and how it can be used in this case?
Sergey Alexandrovich Kryukov 29-Oct-11 21:35pm    
There is no a choice between using stack and heap. If you decide moving some objects to heap, you don't stop using stack -- you always use it.
--SA
Espen Harlinn 30-Oct-11 5:12am    
Good point :)
Espen Harlinn 30-Oct-11 15:41pm    
As you are running out of stack space - you are either developing for a constrained environment such as an embedded controller, or you are doing a serious amount of recursion.

Dynamic allocation from the stack using alloca can seriously improve the performance of a program, but as you have experienced, the stack is a limited resource. free & delete is usually quite expensive operations, while adjusting the stack pointer costs next to nothing ...
ahshabon 29-Oct-11 14:14pm    
After seeing the Boost class, please ignore my question about the shared_ptr.
Also, do not bother answering the stack/heap question.
thanks and have a good day.
thank you all for your input. I realize I was heading in the wrong direction.
here is the issue as it stands now: I use a template function as follows:
template <class t="">
int myFunction(T Epoch)
{
Epoch = (T*) malloc(sizeof(T));
...
}
in main()
{
CEPOCH* cEpoch;
DEPOCH* dEpoch;

if(userinput == 0)
myFunction<cepoch>(cEpoch);
elseif(userinput == 1)
myFunction<depoch>(dEpoch);
...
}
this compiles ok. the pointer inside of myFunction is empty though! what is wrong with this code?
thanks.
 
Share this answer
 
v2
Comments
Nish Nishant 29-Oct-11 10:09am    
You have thanked everyione for their input, but sadly you have not considered following any of the advice. You absolutely need to get a good C++ book and learn some fundamentals before you write any more code.
ahshabon 29-Oct-11 11:40am    
because non of the answers addressed the problem. asking someone to go read a book or follow certain path does not solve the issue. I have been doing C++ programming for over 20 years; I am not a programmer though! if your attitude is to give advice on what I should and should not do, please do not respond to my question.
C++

One of the things covered early on in any Beginner C++ Book is the concept of the constructor and destructor. These are automatically called when you use 'new' and 'delete' to allocate your object.

Your statement,
C++
Epoch = (T*) malloc(sizeof(T));

Becomes,
C++
Epoch = new CEPOCH;


Later, instead of calling,
C++
free(Epoch);


You would call,
C++
delete Epoch;


Ahshabon says, "I have been doing C++ programming for over 20 years; I am not a programmer though!"

I'll forget for a moment that this is contradictory statement...

If you don't know about new, delete, constructors and destructors, then you're not even a beginner yet. Please don't make an appeal to competence in this manner, as many of us, actually have more than 20 years of full time C/C++ work experience.

All of us started us beginners. We read books, we asked questions. We performed code experiments. All we are suggesting, is that you do the same. There are now many good online tutorials, that will help you solve your programming problem. And unlike what some of us older geeks once resorted too, you won't have to search for answers with a 300 baud phone modem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900