Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 247
C++ to C# code conversion issue 4 112
Using Diagnostic tools in VS2015: Unresoved allocations 19 106
gdb doesn't stop on breakpoint 2 95
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 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 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.

828 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