Solved

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

Posted on 2015-01-23
3
137 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
  • 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

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

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

708 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

18 Experts available now in Live!

Get 1:1 Help Now