By Joo


2018-02-13 13:17:56 8 Comments

I have 3 or more struct , and I want that I have one function for print any struct

for example :

struct A 
{
  int a0;
  string a1;
  bool a2;
}

and

struct B
{
 CString b0;
 double b1;
 int b2;
 string b3
}

I want print this struct ( A and B) with same function

like this :

template<typename T>
inline void print(T)
{
  std::cout << // I don't know what is write here....
}

any body help me?

2 comments

@user9335240 2018-02-13 13:47:26

Also, another solution is to make a function called to_string for example (this is to directly convert your struct to string, but the performance is not good)

struct A
{
  int a0;
  string a1;
  bool a2;

  string to_string() const {
      return "{ " + std::to_string(a0) + ", " + a1 + ", " + (a2 ? "true" : "false") + " }";
  }
};

Then

template<typename T>
void print(const T &a) {
    cout << a.to_string() << "\n";
}

This is not good for printing, for printing, use the C++ convention of implementing operator<<

But.

This have a disadvantage, when an subclass want to change the output format. It can't.

So

class DynamicallyToStringConvertible {
public:
    virtual string to_string() const = 0;
    virtual ~DynamicallyToStringConvertible() {}
};

struct A: DynamicallyToStringConvertible
{
    ...
    virtual string to_string() const {
        return "{ " + std::to_string(a0) + ", " + a1 + ", " + (a2 ? "true" : "false") + " }";
    }
}

struct SubclassFromA: public A {
    virtual string to_string() const {
        return "Subclass: " + A::to_string();
    }
};

The function print is as is

Then

void tryPrintingAsA(const A &a) {
    print(a);
}

You will find now (after virtual functions), this will work for SubclassA. But if you tried the operator<< or the beginning of the solution (to_string without virtual), it will work for SubclassA as it was a vanilla A.

Then you would make something like that

friend ostream &operator<<(ostream &s, const DynamicallyToStringConvertible &p) {
    return s << p.to_string();
}

Inside your Base class, like this:

class DynamicallyToStringConvertible {
public:
    virtual string to_string() const = 0;
    virtual ~DynamicallyToStringConvertible() {}

    friend ostream &operator<<(ostream &s, const DynamicallyToStringConvertible &p) {
        return s << p.to_string();
    }
};

Now try the printing function for A to be:

void tryPrintingAsA(const A &a) {
    cout << a;
}

@StaceyGirl 2018-02-13 13:21:15

Usual practice in C++ is to define operator<<(std::ostream &, const T &) for your type:

std::ostream &operator<<(std::ostream &os, const A &value)
{
    // print here
    return os;
}

This should be done for each type you want to print and this function should be defined in the same namespace as that type.

After that, your data types can be printed to all output stream. This also allows things like boost::lexical_cast to work with your type as it prints value to std::stringstream internally.

@Joo 2018-02-13 13:26:35

can you show me, how can use this?

@StaceyGirl 2018-02-13 13:27:58

@Joo Manually for each data type. Were asking for how to iterate over all fields? Because I don't see that being mentioned in the question.

@StaceyGirl 2018-02-13 13:30:27

@Joo If you actually need to print it (e.g. need this for debugging or logging), then I strongly suggest you doing this manually. Custom printing can be way more useful than something automatically generated.

Related Questions

Sponsored Content

14 Answered Questions

[SOLVED] Why can templates only be implemented in the header file?

12 Answered Questions

[SOLVED] typedef struct vs struct definitions

  • 2009-11-04 17:21:57
  • user69514
  • 576447 View
  • 685 Score
  • 12 Answer
  • Tags:   c struct typedef

28 Answered Questions

[SOLVED] When to use struct?

  • 2009-02-06 17:37:55
  • Alex Baranosky
  • 240266 View
  • 1270 Score
  • 28 Answer
  • Tags:   c# struct

23 Answered Questions

[SOLVED] Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

25 Answered Questions

[SOLVED] When should you use a class vs a struct in C++?

  • 2008-09-10 16:29:54
  • Alan Hinchcliffe
  • 345164 View
  • 797 Score
  • 25 Answer
  • Tags:   c++ oop class struct ooad

11 Answered Questions

[SOLVED] Storing C++ template function definitions in a .CPP file

  • 2008-09-22 15:55:52
  • Rob
  • 267671 View
  • 422 Score
  • 11 Answer
  • Tags:   c++ templates

6 Answered Questions

8 Answered Questions

[SOLVED] Difference between 'struct' and 'typedef struct' in C++?

  • 2009-03-04 20:41:12
  • criddell
  • 451576 View
  • 742 Score
  • 8 Answer
  • Tags:   c++ struct typedef

7 Answered Questions

[SOLVED] Pretty-print C++ STL containers

6 Answered Questions

[SOLVED] How to print function pointers with cout?

  • 2010-01-14 14:22:26
  • ibread
  • 39599 View
  • 46 Score
  • 6 Answer
  • Tags:   c++

Sponsored Content