Solved

initializing pointer private member in C++

Posted on 2014-10-16
1
191 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 500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
designing in object programming 12 132
How to Correctly derive class from CWinThread in MFC 4 118
Bitwise and to sum elements 2 54
using interface in TLB 3 31
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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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.

759 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