Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

a reference to an object ? (C++)

To C++ expert :

   Is it legal to have a reference to an object ? The following code won't compile, and I don't understand
class A{
  int i ;
   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 .... ?

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 !!!
  • 3
  • 2
  • 2
3 Solutions
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.

I meant :

A *a_point = new A(1);

meow00Author Commented:
   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 !!!

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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...


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

meow00Author Commented:
Sorry .....
 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 .......

string(s) should be CString(s)


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

    x = s;

   return x;

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

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.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now