pointer to structure?

F

Firewalker

Hello experts,
I have a quick question. Can you create a pointer to structure or just to
classes?

Thank yu very much
 
R

Rob Williscroft

Firewalker wrote in in
comp.lang.c++:
Hello experts,
I have a quick question. Can you create a pointer to structure or just to
classes?

Yes.

There is only *one* difference between struct's and class's and that's
that members of class's are private by default and members of struct's
are public by default.

HTH.

Rob.
 
P

Phlip

Firewalker said:
I have a quick question. Can you create a pointer to structure or just to
classes?

The only difference between struct's and classes are class members are
private by default. But neither classes nor structs occupy memory to point
to. You mean "can you create a pointer to a struct's object, or just to
class objects".

You may point to...

- functions
- class objects
- struct objects
- data primitives (int, float), etc
- arrays (roughly)

You may also "point" to data members and member functions, but that's really
a "smart offset". Look "member pointers" up in the FAQ, via
http://www.slack.net/~shiva/welcome.txt.
 
F

Firewalker

Ohh. That was so helpful. Thank you guys/
Phlip said:
The only difference between struct's and classes are class members are
private by default. But neither classes nor structs occupy memory to point
to. You mean "can you create a pointer to a struct's object, or just to
class objects".

You may point to...

- functions
- class objects
- struct objects
- data primitives (int, float), etc
- arrays (roughly)

You may also "point" to data members and member functions, but that's
really
a "smart offset". Look "member pointers" up in the FAQ, via
http://www.slack.net/~shiva/welcome.txt.
 
E

E. Robert Tisdale

Firewalker said:
Can you create a pointer to structure or just to classes?

No.
A C++ struct or class is a type not an object.
You can only create pointers to objects of some type.
 
S

Sharad Kala

Rob Williscroft said:
Firewalker wrote in news:[email protected] in
There is only *one* difference between struct's and class's and that's

On a side note, the class keyword is superior to struct in one more way. You
can't write - template <struct Mytype>, but template <class Mytype> is
perfectly legal.

:)

Sharad
 
J

Jacques Labuschagne

Rob said:
Yes.

There is only *one* difference between struct's and class's and that's
that members of class's are private by default and members of struct's
are public by default.

And that

struct A{};
struct B: A{};

is the same as

struct A{};
struct B: public A{};

while

class C{};
class D: C{};

is equivalent to

class C{};
class D: private C{};


Regards,
Jacques.
 
P

Pedro Graca

Sharad said:
can't write - template <struct Mytype>, but template <class Mytype> is
perfectly legal.

[newbie]
hmmm ... I want to check if structs are compatible with classes for
templates.

So I wrote a small program and verified that is indeed the case :)

Comments from you gurus appreciated.




#include <iostream>


/* struct */
/* this should be in a header file */
struct S {
int a;
int b;
S operator+(S);
};

/* this should be in a separate code file */
S S::eek:perator+(S x) {
S temp;
temp.a = this->a + x.a;
temp.b = this->b + x.b;
return temp;
}

std::eek:stream & operator<<(std::eek:stream & in, const S & x) {
std::cout << "(" << x.a << ", " << x.b << ")";
return in;
}
/* end struct */

/* class */
/* this should be in a header file */
class C {
int a;
int b;
public:
C(int, int);
C operator+(C);
friend std::eek:stream & operator<<(std::eek:stream & in, const C & x);
};

/* this should be in a separate code file */
C::C(int x, int y) {
this->a = x;
this->b = y;
}

C C::eek:perator+(C x) {
C temp(0, 0);
temp.a = this->a + x.a;
temp.b = this->b + x.b;
return temp;
}

std::eek:stream & operator<<(std::eek:stream & in, const C & x) {
std::cout << "(" << x.a << ", " << x.b << ")";
return in;
}
/* end class */

template<class CLASS_OR_STRUCT>
CLASS_OR_STRUCT test_sum(CLASS_OR_STRUCT x, CLASS_OR_STRUCT y) {
return x + y;
}

int main() {
int ix=6, iy=-5;
double dx=-3, dy=5;
S Sx={5, 0}, Sy={-2, 4};
C Cx(-3, 3), Cy(8, 3);

using std::cout;
using std::endl;

cout << test_sum(ix, iy) << endl;
cout << test_sum(dx, dy) << endl;
cout << test_sum(Sx, Sy) << endl;
cout << test_sum(Cx, Cy) << endl;
}
 
S

Sharad Kala

Pedro Graca said:
hmmm ... I want to check if structs are compatible with classes for
templates.

They are compatible i.e. you can have struct templates like class templates.
So I wrote a small program and verified that is indeed the case :)

The point I was making was that you cannot write this in your sample
program -
template<struct CLASS_OR_STRUCT>
^^^^
CLASS_OR_STRUCT test_sum(CLASS_OR_STRUCT x, CLASS_OR_STRUCT y) {
return x + y;
}

Sharad
 
R

Ron Natalie

Rob said:
There is only *one* difference between struct's and class's and that's
that members of class's are private by default and members of struct's
are public by default.
Yes, and to go farther... structs ARE classes in C++.
 
P

Pedro Graca

Sharad said:
The point I was making was that you cannot write this in your sample
program -
template<struct CLASS_OR_STRUCT>
^^^^


Ah!
To avoid confusions I'll write


template<typename whatever[, ...]> ...


in the future.
Unless, of course, some one here persuades me otherwise.
 
J

Jerry Coffin

[ ... ]
To avoid confusions I'll write


template<typename whatever[, ...]> ...


in the future.
Unless, of course, some one here persuades me otherwise.

One naming system (that I first saw in _Modern C++ Design_) that seems
fairly reasonable to me is to use 'typename' when the parameter can be
any type, and 'class' when it is restricted to a UDF.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top