?
Solved

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

Posted on 2015-01-23
3
Medium Priority
?
168 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 2000 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

765 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