Initialising reference in constructor C++

There is no such thing as an “empty reference”. You have to provide a reference at object initialization. Put it in the constructor’s base initializer list:

class c
{
public:
  c(int & a) : i(a) { }
  int & i;
};

An alternative would be i(*new int), but that’d be terrible.

Edit: To maybe answer your question, you probably just want i to be a member object, not a reference, so just say int i;, and write the constructor either as c() : i(0) {} or as c(int a = 0) : i(a) { }.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)