Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:58:16+00:00 2026-05-29T22:58:16+00:00

I have the following code in my .h/.cpp files: .h: class Foo; typedef Foo

  • 0

I have the following code in my .h/.cpp files:

.h:

class Foo;
typedef Foo * pFoo;

class Foo {
public:
    char c;
};
std::ostream& operator<<(std::ostream &out, const Foo &f);
std::ostream& operator<<(std::ostream &out, const pFoo &f);

.cpp:

std::ostream& operator<<(std::ostream &out, const Foo &f) { out << f.c; return out; }
std::ostream& operator<<(std::ostream &out, const pFoo &f) { out << f->c; return out; }

In main when I run the following code:

Foo f;
f.c = 'a';
std::cout << "As foo object:" << f << std::endl;
std::cout << "As foo pointer:" << &f << std::endl;

I get the output:

As foo object:a
As foo pointer:a

But, if, for instance, I replace my typedef with:

#define pFoo Foo*

Instead I get the output:

As foo object:a
As foo pointer:0x7fff5fbff980

I know you cannot overload operators for built-in types. Is typedef really creating a new type, or is it just an alias for the existing type? The answer seems to be that it is creating a new type. I’m basically looking for a deeper explanation between the difference in behavior. (I’m not trying to do this in production code.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:58:18+00:00Added an answer on May 29, 2026 at 10:58 pm

    Is typedef really creating a new type, or is it just an alias for the existing type?

    typedef introduces aliases or synonyms for types.

    What happens here is that when you use the typedef, const pFoo is a const pointer to a Foo.
    When you just replace pFoo with Foo* using a define, the function argument is const Foo*, which is a pointer to a const Foo.

    Try out the following variations as the parameter type:

    const Foo * &
    Foo const * &
    Foo * const &
    

    Note that as you can always dereference pointers, there shouldn’t be any need for the second overload:

    Foo *p = &f;
    std::cout << *p << std::endl; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
I have the following code #include <iostream> #include <vector> using namespace std; int distance(vector<int>&
let's say I have the following code in A.cpp file: template <typename T> class
I have the following code: [test.h] class MyClass { public: string Name; MyClass(); void
I have the following CPP code snippet and the associated error message: Code snippet
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following code class User attr_accessor :name end u = User.new u.name =
I have following code snippet that i use to compile class at the run
Suppose I have the following files: lib/A.h #ifndef A_H #define A_H #include <vector> class
I have defined the following CComPtr object and method in my class: private: CComPtr<IRawPdu>&

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.