I was looking at this:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.18

I don't understand

File f = OpenFile("foo.txt");

It seems like File is one class, and OpenFile is a separate class, so how can you assign an OpenFile to a File?

Also, is it necessary to have this second class? Why not something like this:

#include <iostream>

class Test
{
	private:
		int A,B,C;
	public:
		Test& setB(const int b)
		{
			B = b;
			return *this;
		}
		
		Test& setC(const int c)
		{
			C = c;
			return *this;
		}
		
		Test(const int a) : A(a) 
		{
			B = 0;
			C = 0;
		}
		
		void Output()
		{
			std::cout << A << " " << B << " " << C << std::endl;
			
		}
};

int main(int argc, char *argv[])
{
	Test T(1).setB(4);
	T.Output();
	
	return 0;
}

(This gives me "unexpected ',', ';', or '.'" on the Test T instantiation)

Any thoughts?

Dave

Recommended Answers

All 3 Replies

>>Test T(1).setB(4);

Should be

Test T(1);
    T.setB(4);

So it's really not that beneficial, you can just write:

T.setB(4).setC(5);

instead of

T.setB(4); T.setC(5);

?

>>T.setB(4).setC(5);

The second method you posted is preferred. The above method is unconventional and may be easily misunderstood and confused by readers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.