Solved

dynamically allocating  an object

Posted on 2006-11-13
7
187 Views
Last Modified: 2010-04-01
I am trying to dynamically allocate an object of type ourClass, please advise if I am correct ?
            typedef ourClass* ourClassPtrType;
            ourClassPtrType ourClassPtr;
            ourClassPtr = new ourClass;
Thanks.
0
Comment
Question by:Gipsy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 6

Expert Comment

by:aescnt
ID: 17933172
Looks just about right. Why don't you try it?
0
 
LVL 6

Expert Comment

by:_iskywalker_
ID: 17933296
i would do a:
ourClassPtr = new ourClass();
if it fails, but i am not sure, for more point i would try more ;)
0
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 20 total points
ID: 17934309
You don't need to create type ourClassPtrType to dynamically allocate an object of class ourClass.

It is usually done like this:

ourClass {
     // definition of your class.
};

int main() {
...
ourClass* ourClassPtr = new ourClass();
// now ourClassPtr is a pointer to a dynamically allocated object of your class.
}

If you realy need type ourClassPtrType, you can do same thing:

ourClass {
      // definition of your class.
};

typedef ourClass* ourClassPtrType;

int main() {
     ourClassPtrType ourClassPtr = new ourClass();
// now ourClassPtr is a pointer to a dynamically allocated object of your class.
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:efn
ID: 17944731
You don't need the parentheses on the new expression.

ourClass* ourClassPtr = new ourClass();
ourClass* ourClassPtr = new ourClass;

These both do the same thing, so the lack of parentheses was not a flaw in the code in the question.
0
 
LVL 6

Expert Comment

by:SeanDurkin
ID: 21427218
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup Zone:
  Accept: UrosVidojevic {http:#17934309}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Sean Durkin
Experts Exchange Cleanup Volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21449007
Forced accept.

Computer101
EE Admin
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question