Solved

a reference to an object ? (C++)

Posted on 2003-10-26
7
288 Views
Last Modified: 2010-04-17
To C++ expert :

   Is it legal to have a reference to an object ? The following code won't compile, and I don't understand
why:
---------------------------
class A{
  int i ;
 public:
   A(int x) : i(x){}
} ;

int main()
{
  A &a_ref = new A(1);
  return 0 ;
}
--------------------------------
 it works when I change "A &a_ref" to
 "const A &a_ref". But I don't understand why can't
  we have a reference to an object .... ?
--------------------------------
similarly,

string a("meow") ;
char *c_ptr = a.c_str() ;

doesn't compile either ...... I need to change
"char *c_ptr" to "const *c_ptr" as well ? why is that ??
-----------------
 Thanks a lot !!!
0
Comment
Question by:meow00
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Assisted Solution

by:marcin1
marcin1 earned 110 total points
ID: 9624503
First example does not work because new operator returns a pointer
to the object, so it should look like this:

A *a_poiner = new A(1);

Now you can access public methods and fields using ->

a_point->i = 5;
(if "i" is public of course)

Or you can dereference the pointer returned by new, using the star

A &a_ref = *(new A(1));

Dereferencing means getting the underlying object from the pointer.

I have no idea why it works when you change "A &a_ref" to
"const A &a_ref", it doesn't work on Borland C++ Builder 6

As for the second part, I don't know which library you use, but
I think the c_str() method of string class returns "const * char",
which is not the same as "char *".

"c_str()" is desinged for reading purposes only, that is why it returns
"const char*" - to prevent from changing the string, what could be
catastrophic in the "string" object.

0
 
LVL 1

Expert Comment

by:marcin1
ID: 9624506
I meant :

A *a_point = new A(1);

Sorry
0
 
LVL 1

Author Comment

by:meow00
ID: 9624645
Hi,
   I know this is a silly question ... but I really don't understand :
 if "new" operator returns a pointer to an object, why we can
 do :
   A a_obj = new A ;
 does the assignment operator " = " implicit do something there ?
 
 Thanks a lot !!!

meow.....
 
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Assisted Solution

by:jcwlc
jcwlc earned 40 total points
ID: 9624763
References were created to make life easier for people who were confused by pointers!

It allows you to use . (DOT) notation instead of -> (POINTER) notation. However, unlike a pointer in C++, you cannot change it's value. Hence, the compiler forces you declare it as constant. You cannot have a reference to a reference, like you can have a pointer to a pointer...

I suggest you stick to one or the other when learning C++... Generally I only use references when passing/returning pointers to objects...

eg:

//returns a copy string object...
CString & func(CString &s)
{
   CString x = new CString(s);
   return x;
}


0
 
LVL 1

Author Comment

by:meow00
ID: 9624822
Sorry .....
but
---------------------------
 string& func(string &s)
{
   string x = new string(s);
   return x;
}
-----------------------
does not compile for me ......
I got error message :
--------------------------
conversion from `string *' to non-scalar type `basic_string<char,strin
g_char_traits<char>,__default_alloc_template<false,0> >' requested
test.C:6: warning: reference to local variable `x' returned
-----------------------------
what does it mean .......
0
 
LVL 1

Expert Comment

by:jcwlc
ID: 9625008
Sorry,

string(s) should be CString(s)

regards,
0
 
LVL 1

Accepted Solution

by:
jcwlc earned 40 total points
ID: 9625317

const CString& func(CString &s)
{
    static CString x;

    x = s;

   return x;
}

CString y("hhh");
CString x = func(y);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

912 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

18 Experts available now in Live!

Get 1:1 Help Now