Intilasing References

Posted on 1997-05-07
Medium Priority
Last Modified: 2010-04-10
How do you intialise refrences, when they occur as member functions.

ie, the case I have boils down to (if you ignore the application specifics)

int j;

class malc
            malc():m_i(* (new int))
            ~malc{delete m_i;}
            int & m_i;

complier says that I need to initiliase the reference to a non temp int.  The best way I thought of was the constructor shown above, with the corresponding destructor.

I don't now if this is the right way to go about it, or if I'm completely on the wrong track.  So if you could fix the above code, or just show me the more correct way of doing it as it just doesn't look right to me, or say my code is wonderful and be done with it :)



Question by:trevena

Accepted Solution

LucHoltkamp earned 100 total points
ID: 1163342
You're code is almost correct! You forgot a '&' in the destructor. I type the code again, how it should be:

struct Test
  int &i;   // the alias, must be initialised in constructor
  Test() : i(*new int) {}
  ~Test() { delete &i; }

One more thing, I'm using BC5 witch is a ANSI-compliant compiler. If your's not, it could be the source of the problem. (Tried to save money ?? ;) )

Author Comment

ID: 1163343
>One more thing, I'm using BC5 witch is a ANSI-compliant compiler. If >your's not, it could be the source of the problem. (Tried to save money ?? >;) )


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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 …
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 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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

624 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