Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

dynamically allocating  an object

Posted on 2006-11-13
7
Medium Priority
?
192 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 80 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
Independent Software Vendors: 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

618 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