working with structs

Hey everyone,

I am fairly new to c++, but I have to use it for my MSc project, however, I stuck in the following problem:

I have two structs that share a variable in their definition for example:

struct A
{ int V;
int W;
int X;
} Struct1;

struct B
{ int X;
int Y;
int Z;
} Struct2;

If variable X in both structs carry the same value, is it possible to pass Struct1 to a function that needs to read Value Z in Struct2??? I mean is there anyway to jump from one struct to another through a common variable??


Thanks


closed account (Dy7SLyTq)
no. you could pass A::X or B::X to an int. what you want to do is called class hierachies. http://www.cplusplus.com/doc/tutorial/inheritance/ it uses classes but it should suffice
Yes and actually that feature is one of the most interesting tools that C++ offers. If both Struct A and B share a similar heritage, either they both inherit from the same class or one inherits from the other, then you get to use the polymorphism feature in this language. Don't be intimidated by the big name though, it basically means that if two object types share the same heritage then you can treat a pointer to either one as if it where an instance of pointer to the parent class. The tutorial here explains these concepts a lot better then I do.

Inheritance: http://www.cplusplus.com/doc/tutorial/inheritance/

Polymorphism: http://www.cplusplus.com/doc/tutorial/polymorphism/
Last edited on
Your question is rather unclear. For example, I have no idea what you mean by " jump from one struct to another ..."

Also, as you define variables immediately after your struct definitions, it looks like they are intended for use in a single scope. This would makes it either (a) impossible to pass via a function, if defined at function scope, or (b) pointless, if defined at global scope.

Anyway...

As your two structs have exactly the same memory layout, as they have the same member types in the same order, it would be safe to reinterpret_cast from a pointer or reference of one type to the other. But when a functions which takes a B& or B* reads the value of X, it will be the value set as V in the original A struct, etc. (see output + code below.)

Notes:

1. This cast "trick" has nothing to do with (subtype) polymorphism, which relies on inheritence and virtual functions (see link to Polymorphism provide by Computergeek01.)

2. You can use upcasting (converting a derived-class reference or pointer to a base-class) and downcasting (converting a base-class pointer or reference to a derived-class pointer) with structs which have an inheritence relationship. But this isn't (subtype) polymorphism, either.

3. I don't think this cast trick is a good thing to do, even though it's possible.

So...

Why do you need the two struct types in the first place? What problem are you trying to solve using this approach?? (A simplistic examples using single letter identifiers doesn't illustrate how this "trick" would be of any real use in real life code.)

Andy

PS C++ supports four type of polymorphism: when used alone, polymorphism is usually taken to mean subtype polymorphism, which uses inheritence and virtual functions and works at runtime. The others are parametric polymorphism (templating), ad-hoc polymorphism (overloading) and coercion polymorphism (type casting.)

variable a:

struct A:
  V = 1
  W = 2
  X = 3

struct B:
  X = 1
  Y = 2
  Z = 3

variable b:

struct A:
  V = 4
  W = 5
  X = 6

struct B:
  X = 4
  Y = 5
  Z = 6


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
51
#include <iostream>
using namespace std;

struct A
{ int V;
int W;
int X;
}; // Struct1;

struct B
{ int X;
int Y;
int Z;
}; // Struct2;

void dumpA(A& a) {
    cout << "struct A:" << endl;
    cout << "  V = " << a.V << endl;
    cout << "  W = " << a.W << endl;
    cout << "  X = " << a.X << endl;
}

void dumpB(B& b) {
    cout << "struct B:" << endl;
    cout << "  X = " << b.X << endl;
    cout << "  Y = " << b.Y << endl;
    cout << "  Z = " << b.Z << endl;
}

int main() {
    A a = {1, 2, 3};
    B b = {4, 5, 6};

    cout << "variable a:" << endl;
    cout << endl;
    dumpA(a);
    cout << endl;
    dumpB(reinterpret_cast<B&>(a));
    cout << endl;

    cout << "variable b:" << endl;
    cout << endl;
    dumpA(reinterpret_cast<A&>(b));
    cout << endl;
    dumpB(b);
    cout << endl;

    cout << endl;

    return 0;
}
Last edited on
note that it's technically undefined behavior, as, for example, g++ when compiled with -Wall -fstrict-aliasing warns:
test.cc: In function 'int main()':
test.cc:38:33: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
test.cc:43:33: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]

Even though the data layout is the same, the types are different, so the optimizer is free from any obligations to expect that writes made through one reference or pointer could change the values read through the other reference or pointer. It's generally safe for what Andy did though.
Last edited on
Thanks everyone for your kind replies, I am sorry that my question was not clear enough.

My project is to modify one of the LTE (4G) MAC layer Schedulers to consider QoS parameters to calculate scheduling priorities. however the code I am modifying simulates the available users by a vector of structs (LteRnti)

std::vector < LteRnti >* targetUes

in its definition, LteRnti struct holds only two values one of them is the node's IP address.

but for my scheduler to work I need more information about the user which I can find at another struct called (Node) that holds all user information including the user IP address (variable X in my previous post!!). so my question is there anyway to use the IP address in "LteRnti" struct for read other user data at the struct "Node".

I am really sorry if my question still not clear enough, but as I said before I am MODIFYING a code not writing a new one, and the only way -I know- to get my work done is to use the LteRnti struct in my function.

Thanks everyone again.

P.S
both struct are defined in separate header files that are included in the scheduler header.
Last edited on
Sorry, but I am still finding it hard to follow.

1. LteRnti is a pre-existing struture

2. There are pre-existing functions that take a LteRnti parameter and cannot be changed.

3. Node is a new structure which cannot be changes either

4. You need to keep all the Node variables in a vector, or similar, too

Obviously I still don't know the details. But, esp. as LteRnti is a small structure, you should look at the possibility of replacing the LteRnti vector with a Node vector (assuming the other of the two values is hold isn't important/lost) and use a temporary LteRnti variable when you call the old, LteRnti based functions.

Andy
@andywestken would you please drop me an email at asa621@hotmail.com so I can explain more and send you related files. as I cannot explain every thing here because I have some copyright concerns!!
Last edited on
Sorry!

I'm happy to answer question here, but I don't go further than that on a non-commercial basis.

Andy
Topic archived. No new replies allowed.