Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I to inherit a struct?

Status
Not open for further replies.

mexicali

Programmer
Jan 20, 2004
5
MX
The example:

struct MY_SOCKADDR public : SOCKADDR_IN{
UINT sin_SocketHandle;
};

works fine in my app but, is legal?, I only want to inherit that structure to add that variable member.

>Please sorry my english;
 
Since a struct is virtually identical to a class in C++, it is legal to inherit from a struct just as you would from a class. That doesn't mean its always good idea as a design decision, but it is definitely legal.

In your example, the colon is on the wrong side of the public keyword. I assume that's just a typo.
 
Yes, it is very possible.
For example I've done a lot before things like

class MyWndClass: public WNDCLASS
{
public:
MyWndClass(){default initialization}
void Register(){RegisterClass(this);}
};


Ion Filipski
1c.bmp
 
Thank you very much by these answers, and thanks for the observation about the wrong side of the colon uolj, I just have not found the way to correct this post, is there some way?.
Ion Filipski thanks for you reply, in your example you inherit from a class and that is one of the advantages of the OOP, but I am not very sure to use the inheritance from a struct, but he is really useful, :)
 
If you read
Code:
struct xxx 
{
   ...
}
as
Code:
class xxx
{
public:
    ...
}
you'll understand what Ion has posted.
 
OK, in conclusion is legal while the compiler does not protest;), but probably isn't a very orthodox metod of programming, at least that is what i think because is not a very used method; Although in C it is the only way to create more sophisticated data types, isn't it.
 
Since you would know what is WNDCLASS structure and you have used it a lot, you will find much manu advantages than you see now. So, it is a standard method of using inheritance.

Ion Filipski
1c.bmp
 
> probably isn't a very orthodox metod of programming, at least that is what i think because is not a very used method

I'm not sure what you mean by that. As was mentioned earlier, a struct is essentially exactly the same as a class, the only difference lying in default access priveleges.

Since inheriting from a class is an orthodox method of programming, inheriting from a struct is, as well. It might not be stylistically used much, with struct often being reserved for simple types with all public members and therefore not prime candidates for being parent classes.

> Although in C it is the only way to create more sophisticated data types, isn't it.

No, especially since inheritance doesn't exist in C, only in C++.
 
>No, especially since inheritance doesn't exist in C, only in C++.
Thanks chipperMDW, je, sorry.

Thanks to all of you people, I see now;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top