Jump to content
  • Advertisement
Sign in to follow this  
pascalosti

struct pointer!

This topic is 4353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#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(){

}

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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;}
};

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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);*/



}

Share this post


Link to post
Share on other sites
@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].

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!