C is equivalent to Java’s TOSTRING?

I want to control the content written to the stream, namely cout, to obtain objects of a custom class. Is this possible in C? In Java, you can override the toString() method to achieve similar purposes.
In C, you can overload the calculation <
class A {
public:
int i;
};

std::ostream& operator<<(std::ostream &strm, const A &a) {
return strm << "A(" << ai << ")";
}

So you can output instances of the class on the stream:

A x = ...;
std::cout << x << std::endl;

If your operator << wants to print out the inside of class A, and really needs to access its private and protected members, you can also declare it as a friend function :

class A {
private:
friend std::ostream& operator<<(std::ostream&, const A&);
int j;
};

std::ostream& operator<<(std::ostream &strm, const A &a) {
return strm << "A(" << aj << ")";
}

I want to control the content written to the stream, i.e. cout, to obtain objects of a custom class. Is this possible in C? In Java, you can override the toString() method for similar purposes.

In C, you can overload the operator<

class A {
public:
int i;
};

std::ostream& operator<<(std: :ostream &strm, const A &a) {
return strm << "A(" << ai << ")";
}

So you can output on the stream Examples of classes:

A x = ...;
std::cout << x << std::endl;

If your operator << wants to print out the inside of class A, and really needs to access its private and protected members, you can also declare it as a friend function:

class A {< br />private:
friend std::ostream& operator<<(std::ostream&, const A&);
int j;
};

std: :ostream& operator<<(std::ostream &strm, const A &a) {
return strm << "A(" << aj << ")";
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2188 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.