0

So, I have a bit of a philosophical question in the realm of C++11 coding best practices.

When creating an application which essentially transforms data from one system to another. Should you define everything in classes, or use structs within a class?

Here is a more concrete example of what I am referring to.

For a typical class object. Should I create it this way:

class Contact {
    std::string Name;
    std::string Address;
    ::       ::       ::
    void Load();
    void Save();
    std::string OtherFunctions( std::string Name )
}

Or is it better to separate out the data from the class:

struct ContactInfo {
    std::string Name;
    std::string Address;
    ::       ::       ::
}

class Contact {
    ContactInfo data;
    void Load();
    void Save();
    std::string OtherFunctions( std::string Name )
}

Several reason that I am contemplating the Struct over just doing in a class is the ability to transfer data between APIs. For example, the creation of these objects is done at a low level pure C++ application. But, at some point through the process, it is exposed to a managed C++ application and then finally consumed in a .NET application.

Secondly, as information is passed around from function to function, I am passing only the information and not the class object. With modern compilers, perhaps this point is mute, but logically to me, it seems better to pass data only than objects.

Thirdly, it separates class members needed to manage the object data from the data itself. So having a members like ErrorCode, ErrorMessage, etc, don't pollute what is considered data and what is not.

Am I off base here? Is there a better way that I should be doing this type of activity?

user3072517
  • 513
  • 1
  • 7
  • 21
  • 2
    the main difference between a `struct` and a `class` is visibility. Beyond that.... it's a personal choice. – Mgetz Jan 28 '15 at 19:14
  • possible duplicate of [When should you use a class vs a struct in C++?](http://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c) – Mgetz Jan 28 '15 at 19:15
  • 1
    If ContactInfo is all the data in a Contact then typically one would not factor it out as a separate class (or struct). A class is the data plus the functions performable on them, so it's ok and idiomatic to have the data be directly in the class. That said I can see that ancillary data may creep into the Contact, like storage path information, creation date, owner etc. If that's the case it may be good design to have the core data separate. – Peter - Reinstate Monica Jan 28 '15 at 19:20
  • Mgetz...I read the topic you suggested, but it seems to waffle back and forth and it was from a generic class usage sense. But for an API development or a translation engine, I was looking for a more definitive answer. I was also wondering if there was some form of inheritance that may solve this as well. This is one case where having multiple inheritance might be of value (which I normally don't do); one for the base class and then one for the data members that are externally used. Thoughts? – user3072517 Jan 28 '15 at 22:51

0 Answers0