struct pointer!

Started by
18 comments, last by intransigent-seal 17 years, 8 months ago

#include <iostream>
#include <string>

using namespace std;


typedef struct person{
	string name;
	string lastname;
	string age;
	string *output;// is it possible to have a pointer point to 
                  //a function to add all other variables together
                 // i know how to do it in main
}dude, dudet;

void AddTogether(person x){
	x.output = x.output + x.name;
}

void main(){

}

Advertisement
Yes, it is possible to have a pointer to a function in C++. See this link, or Google for "C++ function pointer".

Your code also contains at least one bad C++ practice through the obsolete use of typedef struct.
Yes you can have a func pointer, but ask yourself do you need one?
And why are you using a string* ?

Quote:
struct person{
string name;
string lastname;
string age;
string output;
void AddTogether(){output += name;}
};

For starters, don't use the typedef struct idiom: it is a C-ism that does not belong in C++ programs. Don't. Don't. Don't.

Next, in your code you are trying to assign a string to a string*. That's never going to work.

Now for your question:

Quote:is it possible to have a pointer point to a function to add all other variables together.


Yes, it is possible. But since you're using C++, you probably want a member function, don't you? Your code doesn't really let me figure out what you want to do exactly, but I believe you want something like this:

struct person{   string name;   string lastname;   string age;   string output();};string person::output(){   return name + " " + lastname + " " + age;}

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
that worked like a charm thanks

only thing is i have to use typedef or else i get a tone of errors, with typedef i get no errors.

#include <iostream>#include <string>using namespace std;struct person{	string name;	string lastname;	string age;	string AddTogether();}dude;string person::AddTogether(){	 return name +" " + lastname + " " + age + " ";	 }void main(){	//initialize	dude sunny = {"bob", "smith", "45"};	dude funny = {"mic", "white", "55"};	// printed seperatly	cout << sunny.age << endl;	cout << sunny.lastname << endl;	cout << sunny.age << endl;	cout << sunny.AddTogether() << endl;	/*AddTogether(sunny);	AddTogether(funny);*/	}
@OP: Use your typedef, you are correct.

@Everyone: He is correctly using the typedef. A dudet/dude is simply a person, that's what he's using the typedef for [wink].
Quote:Original post by agi_shi
@Everyone: He is correctly using the typedef. A dudet/dude is simply a person, that's what he's using the typedef for [wink].


0[array] is a correct way of accessing the first element of an array. Is it a good thing to do?

Using typedef struct in C++ is bad style, especially when the good style requires less typing.

Quote:Original post by ToohrVyk
Quote:Original post by agi_shi
@Everyone: He is correctly using the typedef. A dudet/dude is simply a person, that's what he's using the typedef for [wink].


0[array] is a correct way of accessing the first element of an array. Is it a good thing to do?

Wow that's cool! I never knew that, thanks (rate++)! As for answering, probably no - it might make your code harder to read. (BTW, where do you guys learn these kind of things?)
Quote:
Using typedef struct in C++ is bad style, especially when the good style requires less typing.


Sorry, I thought typedefining structures so that you get them into the same namespace as other structures (that is, no "struct" before the tag is required) is what you guys meant.

Also, I'm curious, how is this:
struct vector { int x, y, z; };typedef vector vector3d;

Less typing than this:
typedef struct vector { int x, y, z; } vector3d;


Not trying to argue, just want to learn.
Quote:Original post by agi_shi
Also, I'm curious, how is this:
struct vector { int x, y, z; };typedef vector vector3d;

Less typing than this:
typedef struct vector { int x, y, z; } vector3d;


This situation seldom occurs. What is the point of defining dude and dudet? Why not use a single one? A type definition is generally useful when you need a shorter typename, such as

typedef std::vector<void std::vector<int>::*(void)>::iterator It;

Another situation where it is used is when you place a new semantic on top of an existing type (at which point it deserves to be separated anyway), such as:

typedef long long int GUID;

Thus, I was suggesting:

struct vector { int x, y, z; };

Without any additional types defined.

Quote:Original post by agi_shi
Quote:Original post by ToohrVyk
Quote:Original post by agi_shi
@Everyone: He is correctly using the typedef. A dudet/dude is simply a person, that's what he's using the typedef for [wink].


0[array] is a correct way of accessing the first element of an array. Is it a good thing to do?

Wow that's cool! I never knew that, thanks (rate++)! As for answering, probably no - it might make your code harder to read. (BTW, where do you guys learn these kind of things?)
Quote:
Using typedef struct in C++ is bad style, especially when the good style requires less typing.


Sorry, I thought typedefining structures so that you get them into the same namespace as other structures (that is, no "struct" before the tag is required) is what you guys meant.

Also, I'm curious, how is this:
struct vector { int x, y, z; };typedef vector vector3d;

Less typing than this:
typedef struct vector { int x, y, z; } vector3d;


Not trying to argue, just want to learn.


I think he could just do this:
struct vector3d {int x, y, z;};

and save time typing.
hippopotomonstrosesquippedaliophobia- the fear of big words

This topic is closed to new replies.

Advertisement