In C, can structs have member functions?

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
I can't seem to find a definitive answer on this, and everything I am trying is not compiling.

EDIT: I have been experimenting with C++ classes, and I noticed my classes will only compile if they are declared globally (i.e. outside of main()) Why is this?

I am only familiar with C, and am just starting to learn some of C++.
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
Classes are the definitions of objects. You can only create the definition out side functions or within header of other class definitions. I know C++ allows for structs to have member functions but no inheritance or polymorphism.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: Cooler
I know C++ allows for structs to have member functions but no inheritance or polymorphism.

That is not correct. C++ structs are exactly the same as classes except their members/methods are public by default.
 

Kntx

Platinum Member
Dec 11, 2000
2,270
0
71
Originally posted by: Special K
I can't seem to find a definitive answer on this, and everything I am trying is not compiling.

EDIT: I have been experimenting with C++ classes, and I noticed my classes will only compile if they are declared globally (i.e. outside of main()) Why is this?

I am only familiar with C, and am just starting to learn some of C++.

You can create member functions by having function pointers within the struct. The actual functions will be outside the struct.
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
Originally posted by: singh
Originally posted by: Cooler
I know C++ allows for structs to have member functions but no inheritance or polymorphism.

That is not correct. C++ structs are exactly the same as classes except their members/methods are public by default.

your right i was thinking C# .
 

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
Sorry to bump my own thread with another question, but I figured it was better than making a new one. So here is my situation - I have a struct that contains data elements that need to be accessed by all the members of a class. Each member of the class has a pointer to the struct that needs to be initialized. I am having problems initializing it.

At the top you can see the struct declaration.

Next is the stuff for the class that needs to access it.

Below that in the for loop is where I try to initialize the class members' pointers to the struct.



It tells me that there is no matching function in the class.

What am I doing wrong?

EDIT: nevermind, putting the structure definition outside of main() fixed it.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
1) There is no class declaraion for logic_gate

the compiler has no way of knowing if the way you have declared the functions get/set is proper.

2) In the get_struct_ptr function there is no declaration of circuit_node_ptr.

3) In the Set_struct_ptr function there is no declaration of circuit_node_ptr.

4) Nit picking - Number_of_nodes is not defined

5) The class that supports test_circuit is not defined
 

Special K

Diamond Member
Jun 18, 2000
7,098
0
76
Originally posted by: EagleKeeper
1) There is no class declaraion for logic_gate

the compiler has no way of knowing if the way you have declared the functions get/set is proper.

2) In the get_struct_ptr function there is no declaration of circuit_node_ptr.

3) In the Set_struct_ptr function there is no declaration of circuit_node_ptr.

4) Nit picking - Number_of_nodes is not defined

5) The class that supports test_circuit is not defined

I just didn't show any of that stuff in my code above. The reason it wasn't compiling was due to the scope of the structure definition.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Did not notice the additional comment of your in the post above. - Sorry
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,674
146
106
www.neftastic.com
In C, short answer is no, structs cannot have functions. However, structs can have function pointers, thereby giving the illusion of having member functions.

Basically this is all a class is in C++, however "hiding" the function pointers into something more normal. Scope of struct/class has already been mentioned, so I won't bother you with that.