-5

I am doing an assignment for my OOP C++ class. I am currently struggling with initializing a pointer. The directions for my assignment from this part state:

Define a class called FracList with private members fracPtr, a Fraction pointer and size to hold the size of the array of Fraction objects.

My current code, which brings errors, goes like this. Note Fraction is another class.

in FracList.h

private:
    Fraction *fracPtr;
    int size;

in FracList.cpp

void FracList::set_ptr(int* p)
{
    fracPtr = p;
}

Fraction *FracList::get_ptr()
{
    return fracPtr;
}

// Default Constructor

FracList::FracList(int s) : size(s)
{
    if (size > 0)
        fracPtr = new Fraction[size];
    else
        fracPtr = NULL;
}

How do I properly initialize the Fraction* fracPtr pointer variable?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Lipaaaaaa
  • 27
  • 5
  • 4
    Please post a [mcve] and the error messages from the compiler. – R Sahu Mar 06 '19 at 18:32
  • You should probably be using `std::vector` – eerorika Mar 06 '19 at 18:34
  • Cheers R Sahu, I will take a look at that link later and edit or repost after reading it. This is my first time posting on this site. eerorika, we are not using in this program. – Lipaaaaaa Mar 06 '19 at 18:48
  • What is meant by the size variable? Number of elements or size in memory bytes? – priojeet priyom Mar 06 '19 at 19:06
  • 1
    A shot in the dark because if it isn't a problem now, it will be some time soon: [What is The Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – user4581301 Mar 06 '19 at 19:14
  • Side note: `FracList::set_ptr` needs a lot more smarts. For example, what happens to the storage at which `fracPtr` could already be pointing? If you are not required to write setters and getters for this class, I would not. They will break encapsulation unless you write them very carefully. For example, consider this bit of idiot code: `delete[] myFracList.get_ptr();` `myFracList` has no idea that someone else just destroyed its data. Only badness can result. – user4581301 Mar 06 '19 at 19:20
  • 1
    Your `set_ptr()` needs to take a `Fraction*` as input, not an `int*`. It is also leaking memory by not freeing the `Fraction[]` array that `fracPtr` is currently pointing at, before then taking ownership of the caller's pointer (which, in of itself, is bad design). A safer design would be to have a setter that takes a new `size` value instead, and then resize the internal `Fraction[]` array as needed. Don't let the caller provide a new `Fraction*` pointer at all. – Remy Lebeau Mar 06 '19 at 19:24

1 Answers1

1

So what you probably meant is this:

class Fraction{
public:
     Fraction(){}
};

class FracList{
private:
    Fraction *fracPtr;
    int size;
 public:
     Fraction* get_ptr();
    FracList(int s);
    void set_ptr(Fraction* p);
};



void FracList::set_ptr(Fraction* p)
{
    fracPtr = p;
    cout<<"The address of p is:"<< p <<endl;
}

Fraction *FracList::get_ptr()
{
    return fracPtr;
}
int main ()
{
    FracList f1(3);
    Fraction* p=new Fraction;
    f1.set_ptr(p);
    cout<<"the address of p is still:" << p << endl;
}

All that you have to do is to understand how to initialize a pointer. Fraction *fracPtr = p; This is the right way to do it and the parameter that you have to pass at this function is not an int, it should be a pointer to Fraction because you want to give to your data member an address that it will point to. The concept is similar to pointers in c :Pointer initialization concept in c

  • Yes, this is what I meant. I thank you VERY much Valeri for showing me how to initialize a pointer, along with all of the other comments/links everyone else posted. Much appreciated <3 – Lipaaaaaa Mar 06 '19 at 21:38
  • You are right I corrected it now to pointer points to the same address – Valeri Grishin Mar 07 '19 at 13:59