i'm building a Console class(i realy need it). and i don't understand how can i build the Write() procedure:(
heres the struture:
write(varname1[,varnamex])
how can i convert it to cout?
Printable View
i'm building a Console class(i realy need it). and i don't understand how can i build the Write() procedure:(
heres the struture:
write(varname1[,varnamex])
how can i convert it to cout?
I'm still not sure I'm understanding what you mean?Code:cout << varname1 << varname2 << endl;
Are you wanting a function with a variable number of arguments? Have a look at
http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx
Also have a look at
http://www.cplusplus.com/reference/cstdio/vsprintf/
which describes vsprintf which creates a char array from a variable number of parameters.
It might be useful if you could provide some actual code example of what you are trying to achieve.
heres the code that i have for now:
the code isn't complete, but i have 2 problems:Code:#define NewLine endl
void Write( anytype varname1,...) //any type {
int i;
for (i=0; i<NumberOfArguments; i++)
{
cout << ArgumentList[i];
}
}
1 - what type i can use(for use any type);
2 - how i know the number of arguments?(i think that you have in that links... i must see better;))
can you advice me more?Code:void Write( anytype varname1,...) //any type
{
int i;
int num;
va_list NumberOfArguments;
va_start ( NumberOfArguments, num );
for (i=0; i<num; i++)
{
cout << va_arg ( enum, anytype ); //show the variable
}
va_end (NumberOfArguments);
}
I think, You are looking for variable number of arguments. Go through the below post
http://stackoverflow.com/questions/1...arguments-in-c
Before reaching for variable argument lists, look at the syntax for formatted output commonly used with output streams such as std::cout. The basic idea here is to overload operator<< such that a reference to the output stream is the left hand parameter and the object to be printed is the right hand parameter. Then, these calls are chained together by returning a reference to the same output stream.
You can do something similiar for your Console class' Write function, e.g., a user of the class might chain Write calls to write objects of different types to the console in sequence. Exactly how depends on what exactly you want to do and with what syntax.
The "convert it to cout" part is too vague: are you talking about implementing your Write function to write to std::cout?
yes. because the prinf() is more complicated and i want be more simple;)
intead use '<<', my function uses ',' and '(',')' for open and close the function;)
but i don't understand how can i do a variable with any type:(
bagavathikumar:
now i did these code:
like you see, i'm using any type;)Code:template<typename T, typename... Args> void Write(T t, Args... args) // recursive variadic function
{
cout << t;
Write(args...) ;
}
(seems that site isn't comptible with VS2010:()
but i get now 4 errors:
1 - "error C2143: syntax error : missing ',' before '...'";
2 - "error C2061: syntax error : identifier 'Args'";
3 - "error C2783: 'void Console::Write(T)' : could not deduce template argument for '__formal'";
4 - "IntelliSense: no instance of function template "Console::Write" matches the argument list";
any advice?
Where does prinf (printf?) come into the picture? You want to come up with something more simple than what? It almost sounds as if you think std::cout is used together with printf.Quote:
Originally Posted by Cambalinho
Looking at your example in post #3, you effectively want variable length argument list syntax. Unfortunately, if I remember correctly, the variable length argument list feature does not cater to objects of class types that do not satisfy certain requirements.Quote:
Originally Posted by Cambalinho
Furthermore, if you are not adding anything over the standard conventions of output streams in C++, why bother? It sounds as if you are merely planning to change the syntax, and that is a Bad Thing because it means your library users have to learn yet another thing with no value added. The only thing you have going is the vague notion of something "more simple", but sorry this:
is not "more simple" than:Code:write( varname1, varname2, newline,...)
If you want to take a look a project that actually does add value, read up on Boost.Format.Code:std::cout << varname1 << varname2 << '\n';
Using functions with variable number of arguments via the va_ set of macros is one of the more dusky corners of c++ inherited from c. I would strongly advise against using this mechanism if the result can be implemented some other way (eg function or operator overloading).
If you are using c++11 and really, really want functions with variable number of arguments, the answer is probably to use Variadic Templates.
http://www.open-std.org/jtc1/sc22/wg...2006/n2087.pdf
http://www.stroustrup.com/C++11FAQ.h...adic-templates
But before you get bogged down in the detail of implementing these templates, think again if you really need this functinality and that what you want to do couldn't be done some other simpler way.
that tutorial is limited, the 1st argument is the master and tell us, indirectly, how many arguments received:(
i need use the write() like these:
write(var1, var2, "hello world")
the order i don't care, but i don't use strings or arguments for tell me the number of arguments.
note: can i use C++11 in VS2010?
the overloading was the best thing that i found:
thanks for allCode://Write
//1 argument
template<typename Type1>
void Write(Type1 var1) { cout << var1; }
//2 arguments
template<typename Type1, typename Type2>
void Write(Type1 var1, Type2 var2) { cout << var1 << var2; }
//3 arguments
template<typename Type1, typename Type2, typename Type3>
void Write(Type1 var1, Type2 var2, Type3 var3) { cout << var1 << var2 << var3; }
//4 arguments
template<typename Type1, typename Type2, typename Type3, typename Type4>
void Write(Type1 var1, Type2 var2, Type3 var3, Type4 var4) { cout << var1 << var2 << var3 << var4; }
//Read
//empty
void Read(){_getch();}
//1 argument
template<typename Type1>
void Read(Type1 &var1) { cin >> var1; }
//2 arguments
template<typename Type1, typename Type2>
void Read(Type1 &var1, Type2 &var2) { cin >> var1 >> var2; }
//3 arguments
template<typename Type1, typename Type2, typename Type3>
void Read(Type1 &var1, Type2 &var2, Type3 &var3) { cin >> var1 >> var2 >> var3; }
//4 arguments
template<typename Type1, typename Type2, typename Type3, typename Type4>
void Read(Type1 &var1, Type2 &var2, Type3 &var3, Type4 &var4) { cin >> var1 >> var2 >> var3 >> var4; }
i accept more advices;)
Try something like this. Note that variadic templates in c++11 are not yet implemented in MSVS2010 or 2012. :cry:
Code:#include <iostream>
using namespace std;
void write()
{
cout << " ";
}
template <typename A, typename ...B>
void write(A argHead, B... argTail)
{
cout << argHead << " ";
write(argTail...);
}
int main(void)
{
write(1, 2, "qwerty", 4.45, "five", 6);
return 0;
}