473,320 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

pointer to structure?

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

Thank yu very much

Jul 22 '05 #1
11 9362
Firewalker wrote in news:_s********************@rogers.com 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
Firewalker wrote:
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.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #3
Ohh. That was so helpful. Thank you guys/
"Phlip" <ph*******@yahoo.com> wrote in message
news:1D*******************@newssvr31.news.prodigy. com...
Firewalker wrote:
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.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces

Jul 22 '05 #4

"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
Firewalker wrote in news:_s********************@rogers.com 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

Jul 22 '05 #5
Firewalker wrote:
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.
Jul 22 '05 #6
Rob Williscroft wrote:
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.
Jul 22 '05 #7
Sharad Kala wrote:
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::operator+(S x) {
S temp;
temp.a = this->a + x.a;
temp.b = this->b + x.b;
return temp;
}

std::ostream & operator<<(std::ostream & 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::ostream & operator<<(std::ostream & 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::operator+(C x) {
C temp(0, 0);
temp.a = this->a + x.a;
temp.b = this->b + x.b;
return temp;
}

std::ostream & operator<<(std::ostream & 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;
}
--
USENET would be a better place if everybody read:
http://www.expita.com/nomime.html
http://www.netmeister.org/news/learn2quote2.html
http://www.catb.org/~esr/faqs/smart-questions.html
Jul 22 '05 #8

"Pedro Graca" <he****@hotpop.com> wrote in message
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
Jul 22 '05 #9
Rob Williscroft wrote:

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++.
Jul 22 '05 #10
Sharad Kala wrote:
"Pedro Graca" <he****@hotpop.com> wrote in message
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>
^^^^

Ah!
To avoid confusions I'll write
template<typename whatever[, ...]> ...
in the future.
Unless, of course, some one here persuades me otherwise.

--
USENET would be a better place if everybody read:
http://www.expita.com/nomime.html
http://www.netmeister.org/news/learn2quote2.html
http://www.catb.org/~esr/faqs/smart-questions.html
Jul 22 '05 #11
Pedro Graca <he****@hotpop.com> wrote in message news:<sl*******************@ID-203069.user.uni-berlin.de>...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: j0mbolar | last post by:
I was reading page 720 of unix network programming, volume one, second edition. In this udp_write function he does the following: void udp_write(char *buf, <everything else omitted) struct...
6
by: Fernan Bolando | last post by:
What is the best way of passing a structure to a function currently I do by it passing a pointer to the structe. sample code below int main() { struct sample_struct a_struct, *point_struct;...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
19
by: junky_fellow | last post by:
Can the size of pointer variables of different type may be different on a particular architecture. For eg. Can the sizeof (char *) be different from sizeof(int *) or sizeof (void *) ? What...
18
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for...
7
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
8
by: Sam | last post by:
I have a situation occuring in my code and I just can't see to figure out why I have an structure called employee that will put all of the employee id's into a char array set to 10 struct...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.