How do pointers inside a struct work?

So, I have the following:

struct Example {string info, Data* data};

A class named Data incidentally.

vector<Data> this;

Now, the problem. The this-vector has a few objects in it. What I want to do is make an Example variable and make the Data* point to the first member of the this-vector. I thought about making something like this:

Example doesnotwork = {"", 0};
doesnotwork.data = this.at(0);

But it doesn't work. Exactly how do I do it?
Last edited on
this is a C++ keyword. Use another name instead "this".
Oops, didn't think of that, but that's not the issue. I simply made up all the variable names for the sake of the example as I don't have the code at the moment.
Last edited on
> But it doesn't work
It doesn't work is not an error message.

> How do pointers inside a struct work?
Same as outside.

> make the Data* point to the first member of the this-vector
Consider that if the vector makes a reallocation, you'll have an invalid pointer.
1
2
3
#include <iostream>
#include <vector>
DELETED


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>

using namespace std;

typedef union Data{
    int m;
    double u;
    long f;
    
    Data(int kj, double lh, long fg)
    {
        m = kj;
        u = lh;
        f = fg;
    }
    
    Data& operator= (const Data& mydat)
    {
        m = mydat.m;
        u = mydat.u;
        f = mydat.f;
        
        return *this;
    }
};

struct Example
{
    vector <Data> m;
    string k;
    
    Example (string info, Data* data)
    {
        m.push_back(Data(2,-9.32322,-324332));
        k = info; *data = m.back();
    }
    
    ~Example()
    {
        cout << "Deleted!\n";
    }
};

int main()
{
    Data* flv = new Data(0,0,0);
    Example doesnotwork("", flv);
    return 0;
}
Last edited on
┬┐what are you trying to show?
Ah, got it. Of course by doing it the simple way:

Example doesnotwork = {"", &this.at(0)};

instead of

Example doesnotwork = {"", 0};
doesnotwork.data = this.at(0);
@Smac89
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
typedef union Data{
    int m;
    double u;
    long f;
    
    Data(int kj, double lh, long fg)
    {
        m = kj;
        u = lh;
        f = fg;
    }
    
    Data& operator= (const Data& mydat)
    {
        m = mydat.m;
        u = mydat.u;
        f = mydat.f;
        
        return *this;
    }
};
Why are you using a union, isn't this undefined behavior?
I have to say, I've never seen a union written that way.

But it looks like undefined behavior to me as well, because for a union you can only access one "member" at a time, after you've previously written to it. If you write a member then read another, it might blow up.
uhh...union? Found it being used somewhere, decided to experiment. I wouldn't do it that way ofc; that is just an example

E: Why would it not work the way I showed?
Last edited on
Check the output in the links I posted.
Yep, just read about union. Thanks for doing that ^
You can't have a typedef with no alias.
1
2
3
typedef union Data1 {/**/} /*alias*/;
typedef struct Data2 {/**/} /*alias*/;
typedef class Data3 {/**/} /*alias*/;
Recall from C:
1
2
3
4
typedef struct tagData
{
    //...
} Data, *PData;
Data and PData are types, not variables. In C++ you could write tagData and it would have the same meaning as Data.
Last edited on
Topic archived. No new replies allowed.