Solved

dynamically allocating  an object

Posted on 2006-11-13
7
177 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

911 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

26 Experts available now in Live!

Get 1:1 Help Now