Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

initializing pointer private member in C++

Posted on 2014-10-16
1
Medium Priority
?
195 Views
Last Modified: 2014-10-22
class MyClass
{
public:
    MyClass( float f, char a );
private:
    float mFloat;
    char mCharacter;
    bool mBoolean;
    ClassA* mA;
};

MyClass::MyClass( float f, char a ) : mFloat( f ), mBoolean( true ),mA(someobject)
{
}

Would it work if mA was initialized like this? Note: It is a pointer
0
Comment
Question by:LuckyLucks
[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
1 Comment
 
LVL 31

Accepted Solution

by:
Zoppo earned 2000 total points
ID: 40384361
Hi LuckyLucks,

yes, this works, pointers can be initialized this way where 'someobject' has to be a pointer too, the question is where you get the 'someobject' (which must be a pointer too) from.

You can either create a new pointer there or pass a pointer or reference to a ClassA in the constructor, i.e.:

MyClass( float f, char a )  : mFloat( f ), mBoolean( true ),mA( new classA )
{
}

MyClass( float f, char a, ClassA* ptrA )  : mFloat( f ), mBoolean( true ),mA( ptrA )
{
}

MyClass( float f, char a, ClassA& refA )  : mFloat( f ), mBoolean( true ),mA( &refA )
{
}

Note: The last one can even be used (and in fact is the only way) to initialize a member which is a reference instead of a pointer.

Hope that helps,

ZOPPO
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

  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 …
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

704 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