How do i dereference a pointer to something in a class without having to write another function to dereference it for me? For instance in this code:

class Fraction
{
public:
	Fraction(int left = 0, int right = 1);
	Fraction(Fraction & obj);
	~Fraction() { delete fl; }
	double getFraction() { return *fl; }
	void operator=(Fraction & obj);
	friend ostream &operator<<(ostream & out, Fraction & obj);
private:
	int n;  // numerator
	int d;  // denominator
	double * fl;   // pointer to double value which is the decimal equivalence
	               // of the fraction
};

ostream &operator<<(ostream & out, Fraction & obj)
{
	out << obj.getFraction();                // don't want to use function here

	return out;
}

Recommended Answers

All 3 Replies

Dunno, maybe something like this?

out << *(obj.fl);

How do i dereference a pointer to something in a class without having to write another function to dereference it for me? For instance in this code:

class Fraction
{
public:
    Fraction(int left = 0, int right = 1);
    Fraction(Fraction & obj);
    ~Fraction() { delete fl; }
    double getFraction() { return *fl; }
    void operator=(Fraction & obj);
    friend ostream &operator<<(ostream & out, Fraction & obj);
private:
    int n;  // numerator
    int d;  // denominator
    double * fl;   // pointer to double value which is the decimal equivalence
                   // of the fraction
};
 
ostream &operator<<(ostream & out, Fraction & obj)
{
    out << obj.getFraction();                // don't want to use function here
 
    return out;
}

Here's how to difference a pointer, using the "out << obj.getFraction();" as an example:

// If obj pointed to an object of type Fraction
(*obj).getFraction(); 
 
// As a opposed to
obj->getFraction();

Although when overloading an operator it won't work with pointers. Click here reason why: http://www.dgp.toronto.edu/~patrick/csc418/wi2004/notes/PointersVsRef.pdf

Good luck, LamaBot

USUALLY THIS IS NOT A GOOD IDEA. See the problems at the end.
Have a conversion operator in your class.

class Fraction
{
public:
    Fraction(int left = 0, int right = 1);
    Fraction(Fraction & obj);
    ~Fraction() { delete fl; }
    double getFraction() { return *fl; }
    void operator=(Fraction & obj);
    friend ostream &operator<<(ostream & out, Fraction & obj);
//-----------------------------------------
    operator double()
    { return *fl; }
//-----------------------------------------
 private:
    int n;  // numerator
    int d;  // denominator
    double * fl;   // pointer to double value which is the decimal equivalence
                   // of the fraction
};

ostream &operator<<(ostream & out, Fraction & obj)
{
//-----------------------------------------
    out << obj ;  // don't want to use function here
//-----------------------------------------
    return out;
}

Problem with conversion operators is that it gives a license to the compiler to misuse Fraction type as double whenever it wants. I.e. sometimes it'll misuse your operator when you didn't intend it to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.