简体   繁体   中英

How to define an graph of classes in C++ (field types, getters and setters)

I am trying to figure the correct approach on how to write fields as well as getter and setter methods in C++ for classes that represent independent entities and form a graph or.network of objects.

Keeping things simple, Lets say that we want to write the equivalent code in C++ of the following class in Java.

public class Person {

    private String name; // 1
    private Address address; // 2
    private List<Account> accounts = new ArrayList<>(); // 3

    // 1
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    // 2
    public Address getAddress() { return address; }
    private void setAddress(Address address) { this.address = address; }

    // 3
    public List<Account> getAccounts() { return accounts; }
}

class Address { ... }
class Account { ... }

As you can see I have chosen the standard String type and 2 custom classes as member fields. That is because they are complex types. Besides the name field, the other 2 fields are chosen to demonstrate a typical a many-to-one relationship (person-adress) and one-to-many relationship (person-accounts).

In addition the String type in C++ (std:string) is a RAII type. It used typically in a literal way as it encapsulates the management of its actual value in the heap.

Now, it is not clear to me what would be the correct type definition for each field and how to expose them with getters and setters, while keeping each object's lifetime, in C++ as in the Java example above.

class Person {

    std::string _name;
    // address as pointer? reference? value? shared_ptr?
    // the same as above for the collection of accounts

    public:
    // appropriate getters, setters ?
}

NOTE 1: I am not interested in answers like "why are you doing this?" or "why do you want to do this?". The point of the question is to establish a clear understanding of how we can build a graph of objects in C++. There are plenty legitimate reasons and applications of this, such as designing a domain model, etc. So yes thanks for stinking to the point.

First of all, you are conflating different things. I'll ignore the part of getters and setters for a moment...

// address as pointer? reference? value? shared_ptr?

If you want a std::string then you use a std::string . If you want an Adress then you use an Adress . If you want a value then you use a value.There is no indication in your code that you need anything else. Most lifetimes are mangaged automatically in C++ (and deterministically in constrast to Java).

I am not perfectly certain but I guess a good replace for ArrayList is a std::vector . However, don't confuse them to be the same or even similar. An ArrayList<whatever> contains Java Object s while a std::vector<std::string> does contain acutal std::string s. Values.

struct Adress {
    std::string city;
};
struct Account {
    std::string password;
};

struct Person { 
    std::string _name;
    Adress adress;
    std::vector<Account> accounts;
};

This is all you need to correctly manage lifetime of the objects. Destructors are called automatically.

For passing parameters to setters and returning values from getters you can look at the C++ coreguidelines . However, that part of the guidelines can be a bit overwhelming and basically all you need to know to get started with getters and setters you can find here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM