François
François

Reputation: 473

C++: Local typedef returned by a private method

The code below isn't correct and I understand why, get_point returns a value whose type is unknown outside the class:

class C {
    typedef std::pair<double, double> Point;
 public:
    Point get_point() const;
};

Point C::get_point() const {
    [...]
}

But why isn't the code below correct? The local type isn't used outside the class!

class C {
    typedef std::pair<double, double> Point;
 private:
    Point get_point() const;
};

Point C::get_point() const {
    [...]
}

When we use C:: we should be inside the class, so we should be able to use Point!

Upvotes: 2

Views: 1018

Answers (3)

Niall
Niall

Reputation: 30604

As a brief answer to the question (detailed in the other answers), in the definition of;

Point C::get_point() const {
  //...
} 

Needs to be;

C::Point C::get_point() const {
  //...
} 

Such that the Point in the scope of C is found.

It is worth noting, that if the typedef is private, the name of the type (being Point) can't be used outside of the class. Client code will either need to use auto (or otherwise specify the type).

As per the following sample;

class C {
    typedef std::pair<double, double> Point;
public:
    Point get_point() const;
};

C::Point C::get_point() const {
    return Point(); // dummy code to compile sample
}

int main()
{
    C c;
    //C::Point a = c.get_point(); // fails to compile (would compile if Point typedef was public
    auto a = c.get_point(); // compiles
}

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

It is a valid code. Here is a demonstrative program

#include <iostream>
#include <utility>

class C 
{
    typedef std::pair<double, double> Point;
    Point p { 10.10, 20.20 };
 public:
    Point get_point() const;
};

C::Point C::get_point() const { return p; }

int main() 
{
    C c;

    auto p = c.get_point();

    std::cout << p.first << ' ' << p.second << std::endl;

    std::pair<double, double> p2 = c.get_point();

    std::cout << p2.first << ' ' << p2.second << std::endl;
}

The program output is

10.1 20.2
10.1 20.2

You may not only use name C::Point that is an alias for type std::pair<double, double>.

As for this code

class C {
    typedef std::pair<double, double> Point;
 private:
    Point get_point() const;
};

Point C::get_point() const {
    [...]
}

then you have to write

C::Point C::get_point() const {

An unqualified name used as the return type of a class member function defined outside the class definition is searched in the scope where the class is defined. And there is no name Point. You have to use a qualified name.

Upvotes: 5

Andreas Vennstr&#246;m
Andreas Vennstr&#246;m

Reputation: 715

The definition of the function C::get_point() is in the global scope and Point isn't, so you'll have to specify where Point is located. If you move the definition into the class you don't have to specify the scope of the typedef (C::) when using Point:

class C {
    typedef std::pair<double, double> Point;
public:
    Point get_point() const {
        return Point( 1.0, 2.0 );
    }
};

Upvotes: 2

Related Questions