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 8113857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:58:06+00:00 2026-06-06T02:58:06+00:00

Today, I was curious to find some of differences between a structure and a

  • 0

Today, I was curious to find some of differences between a structure and a class, in C++. So, I found some of the differences:

  1. In a structure, by default members are public while private in class.
  2. Inheritance in case of a structure is public by default, while private in case of class.
  3. Classes can take part in templates, while structures cannot.

click here to see that a struct cannot be used in place of class in case of template.
http://ideone.com/p5G57

template<struct T> void fun(T i)
{
    cout<<i<<endl;
}

int main()
{
    int i=10;
    fun<int>(i);
    return 0;
}

It gives the errors:

prog.cpp:4: error: ‘struct T’ is not a valid type for a template constant parameter
prog.cpp: In function ‘void fun(T)’:
prog.cpp:4: error: ‘i’ has incomplete type
prog.cpp:4: error: forward declaration of ‘struct T’
prog.cpp: In function ‘int main()’:
prog.cpp:12: error: no matching function for call to ‘fun(int&)’

However, if struct is replaced with class, it works perfectly. see here: http://ideone.com/K8bFn

Apart from these above differences, when I replace class with struct in my code, the code works perfectly without making any further changes.

Now, I want to know, are there more differences, that I am missing and I should know?

  • 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-06T02:58:08+00:00Added an answer on June 6, 2026 at 2:58 am

    There’s no other difference, but the third one you specify isn’t correct:

    Class can take part in template while structures cannot.

    In case of templates, the class keyword is just syntactic sugar, it doesn’t mean the type has to be an actual class. Generally, programmers prefer typename for basic types and class for classes or structs, but that’s just by convention.

    Other than that, you can use both class and struct to specialize templates.

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

Sidebar

Related Questions

Today I looked at the ZipEntry class and found the following: public class ZipEntry
I encountered some curious behavior today and was wondering if it is expected or
Today I made some tests and I am curious of the results. I made
Today, while I was randomly reading the JavaScript patterns O'Reilly book, I found one
Working on some code today, I found that the following would work in 5.3,
I realized today while writing some Python that one could write the inequality operator
Came by a curious case today, that got me thinking about how the object
Today, I heard of Karatsuba algorithm, a fast multiplication algorithm. I am curious that
Today, on EFNet C++ Wiki in article heap corruption , I found two pieces
This is something curious that I saw in my coding today. Here is 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.