Accessing Struct from One Class to Another

accessing struct from one class to another

The structure in class A defines a type (that can be used within the scope of class A), but you need an instance of it to be able to call the member functions of the structure. E.g.:

class A {
struct StructOfClassA {
func returnLetterA() -> String{
return "a"
}
}

var structOfClassA = StructOfClassA()
/* Instance of 'StructOfClassA' structure type */
}

class B {
let classA = A()

init() {
let myLetter = classA.structOfClassA.returnLetterA()
print(myLetter)
}
}

var myB = B() // prints "a"

Alternatively, you can let B be a subclass of A, which gives you access to the type StructOfClassA from the superclass, in which case you could create an instance of StructOfClassA and access its method returnLetterA():

class A {
class StructOfClassA {
func returnLetterA() -> String{
return "a"
}
}
}

class B : A {
let classA = A()

override init() {
let myLetter = StructOfClassA().returnLetterA()
print(myLetter)
}
}

var myB = B() // prints "a"

C++ pass struct from from one class to another class

The solution is simple, what you wrote is a bad habit.

There is no reason to include B.h in A.h because you don't use any part of B.h in the A.h file. A better way to implement it is to include B.h in A.cpp and not in the header file.

It will also solve the cycle of includes you have..

In general, it is recommended to include files in .cpp file and NOT in header files when you don't use functions/objects of the included file in the header file :)

In addition, you should use #pragma once for Windows or ifndef for anything else in order to be safe of conflicts

A.h

#ifndef _A_H
#define _A_H
namespace common {
class A {
public:
static struct strctOfA {
float p1 = 2;
} structA;
}
}
#endif

A.cpp

#include A.h
#include B.h
using namespace common;
class A {
B b;
b.functionOfB(structA);
}

NOTE: from B.cpp include A.h

Using a struct across classes in c++

You have to include the header file that contains the definition of the struct everywhere you use it.

The only cases where you don't have to is when you are only declaring references or pointers to it; in that case you can just forward declare it with:

struct details;

Also in C++ you can just declare it with:

struct details{
std::string name;
std::string address;
};

There's really no need for the typedef.

How to access a struct from another another C++ class?

Since TreePair is nested inside TreeItem, it needs to be

QVector<TreeItem::TreePair> rootData;

Pass a struct to another class

Well, for one thing, if you intend to pass the structure to another class, you ought to make the structure definition public (or at least internal)... once you do that, you can use all sorts of methods (properties, method calls, etc) to copy the data over to your other class.

Two techniques are shown below (of course, you'd only need to use one...)

public class Foo1
{
public struct Bar
{
string A;
string B;
}

private Bar[] data;

// Using a method
public Bar[] ExportData()
{
return data;
}

// Using properties
public Bar[] DataProperty
{
get { return data; }
}
}

public class Foo2
{
private Foo1.Bar[] data;

// Using a method
public void ImportData(Foo1 source)
{
this.data = source.ExportData();
}

// Using properties
public Foo1.Bar[] DataProperty
{
get { return data; }
}

public void ReadProperty(Foo1 source)
{
this.DataProperty = source.DataProperty;
}
}

C# - Passing struct from one object to another

Have you defined the actual struct inside each of the classes?

Even if you use the same name on them, the compiler won't treat them as the same type.

You have to define the struct in one place, and make object of it from both classes that needs to use it.

An example

public class A{
public struct MyStruct{
...
}
}

public class B{
public struct MyStruct{
...
}
}

A.MyStruct struct1 = new B.MyStruct();

This is not allowed, and this is actually what you are trying to do. I suggest moving the struct out of the class and put it somewhere both classes can access it.

Define it like this instead

public struct MyStruct{
...
}

public class A{
...
}

public class B{
...
}

MyStruct struct1 = new MyStruct();


Related Topics



Leave a reply



Submit