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

  • Home
  • SEARCH
  • 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 8898361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:35:44+00:00 2026-06-15T00:35:44+00:00

Can typedef FooBar Bar; and the access to the type FooBar through the expression

  • 0

Can typedef FooBar Bar; and the access to the type FooBar through the expression Foo::Bar in the code

#include <typeinfo>
#include <iostream>

class FooBar {};
class FooBat {};

class Foo
{
public:
    typedef FooBar Bar;
    typedef FooBat Bat;
};

int main()
{
    if( typeid(Foo::Bar) == typeid(FooBar) && 
        typeid(Foo::Bat) == typeid(FooBat) )
        std::cout << "All is well." << std::endl;
}

be translated to Java?

What would be the Java equivalent for an indirect reference to a type?

The STL and boost are filled with code such as

typedef T              value_type;
typedef T*             iterator;

and I am wondering whether Java supports a similar generic programming idiom.
I am still interested in an answer even if the type indirection cannot be done at compile time.

Edit
The question (how to do nontrivial generic programming in Java) is not getting any interest from those conversant in Java. I am now adding “C++” as a tag.

  • 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-06-15T00:35:45+00:00Added an answer on June 15, 2026 at 12:35 am

    The C++ program in the question translates to the following Java code:

    public class FooBat {}
    public class FooBar {}
    
    public class Foo {
        public static Class get_Bar() { return FooBar.class; }
        public static Class get_Bat() { return FooBat.class; }
    }
    
    public class Introspect {
        public static void main(String[] args) {
            if( Foo.get_Bar() == FooBar.class &&
                Foo.get_Bat() == FooBat.class )
                System.out.println( "All is well.\n" );
        }
    }
    

    This is not as efficient as the C++ code. The types are determined in the C++ version during compilation. In the Java version they are determined at run-time.

    A better answer that remedies this issue is most welcome.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't get it. Using g++ compiler. Code: #include <iostream> using namespace std; typedef void
can someone explain me the compilation error in this code: #include common.h typedef struct
How can we access variables of a structure? I have a struct: typedef struct
Sometimes, C++'s notion of privacy just baffles me :-) class Foo { struct Bar;
Can anybody tell me when to use typedef in C? In the following code
Given the code in Visual Studio 2010: void Foo::Bar() const { map_t::const_iterator iter(my_map_.find(key)); if(my_map_.end()
typedef struct tnode { ... } Treenode; Since typedef can't create a new type
Why typedef can not be used with static? For example, the code below is
Given: typedef type-declaration synonym; I can see how: typedef long unsigned int size_t; declares
Can you explain for me what the typedef here is doing and what the

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.