maheshgupta024
maheshgupta024

Reputation: 7877

const pointer return the reference

I have a singleton class with a variable that every other class instance is going to use. Now I want to put a function in my singleton class, say "GetReference".

  1. Is it possible to return a reference to a variable? If so, how can I return the reference to the variable?
  2. How do I make others only use and not modify or delete the variable. Will const work for my case?

Upvotes: 1

Views: 516

Answers (5)

maheshgupta024
maheshgupta024

Reputation: 7877

#include <stdio.h>
using namespace std;
//This is the class, which I want to keep others to delete or modify
class Ref{
public:
  int X;
};


class SingleTon{

private:
  static Ref reference;
public:
  SingleTon(){
   // reference = new Ref();
  }
  static const Ref& getReference(){return reference;}
};

int main(){
  Ref * ptr = SingleTon::getReference();
}

If I say SingleTon::getReference(), I should get the refernce of Class Ref, so that everyone only should use it but not modify the contents or delete the pointer.

Upvotes: 0

Mario
Mario

Reputation: 36487

You can return a reference, but only if it's static or part of the object (i.e. no local variable).

You can also return a reference to the object of the class:

class Singleton
{
private:
    static Singleton *singleton = 0;
    Singleton() // making the constructor private keeps others from creating their own instance
    {
    }

    ~Singleton() // making the destructor private keeps others from destroying their instance on their own
    {
    }
public:
    static Singleton *GetPtr(void)
    {
        if(!singleton)
            singleton = new Singleton();
        return singleton;
    }

    static Singleton &GetRef(void)
    {
        return *GetPtr(); // dereference the pointer
    }

    static void Free(void)
    {
        if(!singleton)
            return;
        delete singleton;
        singleton = 0;
    }
}

You could as well return a const pointer and/or reference, depending on what you want to do with your class (as I don't know if you'd just want to keep others from deleting or modifying). Just keep in mind that there are ways to trick this (const_cast<>()).

Upvotes: 0

dimitri
dimitri

Reputation: 954

Strictly speaking const modifier can be casted out and variable modified. Return by value is a safer and better choice than a reference to internal variable.

If return by value is expensive (for example an object of a large class is returned) a delegation pattern can be used with a simple wrapper class and a private reference to the implementation.

Upvotes: 0

Joe
Joe

Reputation: 57179

The const modifier will work for you. In the following example instance/static variable x would not be able to be modified by anything that calls getReference.

const int& Singleton::getReference() const
{
    return x;
}

Upvotes: 0

Xavier V.
Xavier V.

Reputation: 6448

1) To return a reference on a variable, use this kind of syntax :

int& getARefOnDummy() 
{
     return dummy;
}

2) To return a const ref (that will not be able to be modified or deleted), use this kind of syntax :

const int& getARefOnDummy() 
{
      return dummy;
}

Upvotes: 2

Related Questions