0

I'm currently having

Type1 &GetType1() const
{
    return *this->type1;
}

void SetType1(const Type1 &type1)
{
    *this->type1 = type1;
}

and in the class definition

class Type2
{
    public:
        Type2();
        virtual ~Type2();
        Type1 &GetType1() const;
        void SetType1(const Type1 &type1);
    private:
        Type1 *type1 = nullptr;

}

And in main

 int main()
 { 
       Type2 *type2 = new Type2();
       Type1 *newType1 = new Type1();
       type2->SetType1(*newType1);

       delete type2;
       delete newType1;
  }

everywhere in my project. It seems to me that this is not a very safe method, there are cases in which the object is pointing to NULL, etc.. I would like to know if there is a better commonly accepted way to do that. Maybe opperation overloading is a good idea?

Phylax Wetos
  • 173
  • 7
  • 2
    Please provide your class example. Your setters and getters are weird. This post might help you http://stackoverflow.com/questions/760777/c-getters-setters-coding-style . – coincoin Jun 03 '15 at 09:09
  • It helps a bit but it does not cover things like when the object is not initialized and points to nullptr. – Phylax Wetos Jun 03 '15 at 09:11
  • How could your object point to nullptr ? Are you using pointer (you should maybe not) ? As I said can you provide a class example ? – coincoin Jun 03 '15 at 09:12
  • I second coincoin, Provide a [mcve](http://stackoverflow.com/help/mcve). – StoryTeller - Unslander Monica Jun 03 '15 at 09:12
  • Yes, i just did :). School WiFi is slow. – Phylax Wetos Jun 03 '15 at 09:15
  • 2
    Your code is oversimplified and has a lot of errors. I doesn't compile and it does not help to make clear what you are trying to achieve. Maybe try to explain (and understand) what *Do stuff* means. – Wolf Jun 03 '15 at 09:37
  • Why does `Type2` want to keep a pointer to a caller-owned `Type1`, rather than keeping a copy in its own `Type1` object, or a reference to the caller's object? i.e. is it really necessary to both avoid copying the object and allow changes during the lifetime of a `Type2` object? If using a pointer really is functionally necessary, using a `std::shared_ptr` would be a safer way to ensure the stored pointer remained valid as long as the `Type2` object storing it. – Tony Delroy Jun 03 '15 at 09:57
  • Do stuff was just everything i did not show for simplicity it has nothing to do with the problem. I just wanted to know about getters and setters. But about storing the reference inside the class, that would also work. – Phylax Wetos Jun 03 '15 at 12:35

1 Answers1

3

If your class has a member pointer that can be null, then simply return the pointer from the getter function and have the user worry about the corner cases.

Type1* GetType1(){
  return this->type1;
}

void SetType1(Type1* type1) {
  this->type1 = type1;
}

If, by any chance the member cannot actually ever become null, which is a class invariant, then I think it is a good practice to return a reference, and use assertion in the getter.

tsuki
  • 907
  • 4
  • 17