Solved

3. Is it allowed to declare pointer to an object? Pointer to a class?

Posted on 2015-01-23
3
163 Views
Last Modified: 2015-01-23
Is it allowed to declare pointer to an object? Pointer to a class?
0
Comment
Question by:Nusrat Nuriyev
[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
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40567414
Yes, sure. 'But: A pointer to an object is always a pointer to an instance of a class. There is no usch thing as a pointer to a class. It just works like

class MyClass {

  // ...
};

int main() {

  MyClass* pointer_to_an_object_of_myclass = new MyClass()]; // declares and instantiates an objects of 'MyClass' via a pointer
}

Open in new window


As opposed to array, you can specify which constructor should be used with 'new'.
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40567523
The close square brace is a mistake here:
MyClass* pointer_to_an_object_of_myclass = new MyClass()];

So , we can not specify constructor while declaring array of objects?
Big thanks to you valuable input.

Another clarifications from another experts are appreciated.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 40567530
>>The close square brace is a mistake here:

Yes, sorry - typo :-/

>>So , we can not specify constructor while declaring array of objects?

No, thast's not possible, for a simple reason: Constrructors are chosen by their parameters. And when instantiating an array, there is no way to specify any parameters to be passed to any constructor. That's why the default constructor is the only one that applies here. Speaking of appying, the same is true when you use 'new' to create an array and assign it to a pointer, e.g.

MyClass* arr = new MyClass[42];

Open in new window


Here you also can't pass any parameters, so the task of initializing the objects is left to the default constructor.
0

Featured Post

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!

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

726 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