Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

778 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