Solved

Intilasing References

Posted on 1997-05-07
2
218 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

831 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