Solved

dynamically allocating  an object

Posted on 2006-11-13
7
175 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
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now