Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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:
  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:
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 28-Oct-11 7:33am
Comments
SAKryukov at 28-Oct-11 12:47pm
   
OK, and where are input and process?
--SA
CPallini at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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
  Permalink  
v6
Comments
Richard MacCutchan at 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.
SAKryukov at 28-Oct-11 13:41pm
   
:-)
Thank you, Richard.
--SA
Espen Harlinn at 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 :)
SAKryukov at 29-Oct-11 21:30pm
   
Your point taken. :-)
Thank you, Espen.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Probably OOP provides a better way to solve your problem. Have a look, for instance, at Creational Design Patterns at Wikipedia[^].
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Comments
SAKryukov at 28-Oct-11 15:50pm
   
It looks like you are right, my 5.
--SA
Nishant Sivakumar at 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 at 29-Oct-11 11:46am
   
Can you please keep this advice to yourself? this does not help!
SAKryukov at 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 at 29-Oct-11 21:43pm
   
CAN YOU STOP YOUR SILLY COMMENTS? ENOUGH IS ENOUGH!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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
  Permalink  
Comments
ahshabon at 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?
SAKryukov at 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 at 30-Oct-11 5:12am
   
Good point :)
Espen Harlinn at 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 at 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.
Espen Harlinn at 30-Oct-11 15:29pm
   
It was the only thing that made some kind of sense, and you didn't go out of your way to provide a rationale behind the contraption, in the light of
CEPOCH(void* p){p = this;}
SAKryukov at 29-Oct-11 21:38pm
   
Interesting points, my 5.
--SA
Espen Harlinn at 30-Oct-11 5:12am
   
Thanks Sergey :)
Amir Mahfoozi at 30-Oct-11 15:15pm
   
Have another +5
Espen Harlinn at 30-Oct-11 15:43pm
   
Thank you, Amir :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
int myFunction(T Epoch)
{
Epoch = (T*) malloc(sizeof(T));
...
}
in main()
{
CEPOCH* cEpoch;
DEPOCH* dEpoch;
 
if(userinput == 0)
myFunction(cEpoch);
elseif(userinput == 1)
myFunction(dEpoch);
...
}
this compiles ok. the pointer inside of myFunction is empty though! what is wrong with this code?
thanks.
  Permalink  
v2
Comments
Nishant Sivakumar at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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,
Epoch = (T*) malloc(sizeof(T));
Becomes,
Epoch = new CEPOCH;
 
Later, instead of calling,
free(Epoch);
 
You would call,
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 31 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100