Solved

Intilasing References

Posted on 1997-05-07
2
213 Views
Last Modified: 2010-04-10
Hi,
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
      {
      public:
            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 :)

Thanks

Malcolm



0
Comment
Question by:trevena
2 Comments
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 50 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 ?? ;) )
0
 

Author Comment

by:trevena
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 ?? >;) )

No
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now