Solved

dynamically allocating  an object

Posted on 2006-11-13
7
180 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SendMessage fails while PostMessage works 2 114
Parsing XML instructions in Java/any other language.... 3 110
Dialogbox API leak? 18 96
Precision Problem in C++ 7 33
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…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

803 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