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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to convert C++ class in to C struct ??

Status
Not open for further replies.

anand102

Programmer
Aug 2, 2001
11
US
hi,

I have existing C++ classes and I have to create corresponding "structs" in C for them.
how do we do that:

for e.g.

i have one class as

class Anand
{
private:
ClassA a;
public:
static const char * b;
Anand()
{
};
~Anand()
{
};
virtual int getxyz()
{
//return some integer
};
classA getName()
{
// return some object of type classA
};
};

what will be the corresponding struct to this class ??


thanks
pls also tell me few good links about C and C++.



 
this is a rough template which i think would
give you some idea ,remember you can write fns
inside a struct but in standard c program you shouldnot

other thing is concept of virtual function is not
appicable in c for in c there is always early binding

last thing whenever you call a function and you want
it to be called only by the struct type(class)give
the first argument(convention) as a pointer to the struct
type

hope it helps

struct Anand
{
struct A a;
static const char * b;
}
void initialize(Anand* anand)
{
anand->data//initialize here
};
void cleanAnand(Anand* anand)
{
//use delete or free in case of dynamicstorage allocation
//act as the destructor of your class

}


int getxyz(Anand* anand)
{
//return some integer
}
struct A getName()
{
// return some object of type classA
}


correct me if i am wrong
bye
yogesh
 
you can do one thing. That is you can decleare some function pointer variables within the structure but you can not give access specifiers, constuctors, destructors, virtual and more major thing is you have to store the address of the functions in each object.

#include <stdio.h>
struct test
{
char name[10];
void (*ReadNameP)(struct test *);
void (*ShowNameP)(struct test *);
};
void ReadName(struct test *t)
{
scanf(&quot;%s&quot;, t->name);
}
void ShowName(struct test *t)
{
printf(&quot;\n%s&quot;, t->name);
}
int main()
{
struct test One;
One.ReadNameP = ReadName;
One.ShowNameP = ShowName;
(*One.ReadNameP)(&One);
(*One.ShowNameP)(&One);
return 0;
}

If you create another object then once again you have to assign the address of the functions to member pointer variables otherwise you can copy the first object two the new object ( It will include the value of variable name also).

Maniraja S
 
thanks Yogesh and Mani,

so what is deal here ...should i declare function pointers also within Struct as Mani said or I should just have data members in Struct declaration and code functions out side of struct and pass pointer to structs with in function arguments ??


thanks

 
anand

having function pointers inside struct is of no
use because unnecessarily you are increasing the
size of struct and it serves no purpose because
other than calling the function in other way around
you achive nothing using function ptrs inside struct
in c

in cpp public and private are all compile time behaviour
we can easily cheat the language but is there any point


i hope it helps ,

bye
yogesh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top