[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

[c++] nedarve fra stringstream



Hej
Jeg har to udfordringer med noget kode der skal implementere logging funktionalitet. En smule kode:


-------------------
#include <iostream>
#include <string>
#include <sstream>

class LogUnit : public std::stringstream {
public:
  virtual ~LogUnit();
};

LogUnit::~LogUnit(){
  std::cout << str();
}

int main (int args, char* argv) {
  LogUnit() << "test" << " " << "program" << std::endl;
}

---------------------
Ideen er at objektet af typen 'LogUnit' udskriver sit indhold, idet objektet destrueres. Og det sker automatisk når linien er udført.


Problem 1:
Ovenstående program udskriver:
'0x8049928 program'
Den første streng "test" bliver til en pointer. Hvorfor?

Det løser problemet at indsætte noget "usynligt", f.eks.:
LogUnit() << std::flush << "test" << " program" << std::endl;

Problem 2:
Log idéen giver mest mening hvis LogUnit objektet kan have subclasser der opfører sig anderledes, f.eks skriver til en fil i stedet for stdout.
Men g++ giver ikke lov til at en factorymetode returnerer en LogUnit, kun en LogUnit pointer, og så ryger idéen med at LogUnit er et midlertidigt object som destrueres efter liniens udførelse.


Hvordan kan jeg opnå det ønskede?

Uddybning:
Hvis jeg tilføjer factorymetoden:

LogUnit LogFactory::log(){
  return LogUnit();
}

får jeg nedenstående kompiler fejl (som vist blot siger at jeg ikke kan kopiere et stringstream object)

/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/ios_base.h:779: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd:55: error: within this context
ios_test2.cpp: In copy constructor ‘LogUnit::LogUnit(const LogUnit&)’:
ios_test2.cpp:6: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/streambuf: In copy constructor ‘std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::basic_stringbuf(const std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >&)’:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/streambuf:781: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]’ is private
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd:71: error: within this context
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd: In copy constructor ‘std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(const std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >&)’:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/iosfwd:83: note: synthesized method ‘std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::basic_stringbuf(const std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >&)’ first required here
ios_test2.cpp: In copy constructor ‘LogUnit::LogUnit(const LogUnit&)’:
ios_test2.cpp:6: note: synthesized method ‘std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(const std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >&)’ first required here
ios_test2.cpp: In function ‘LogUnit log()’:
ios_test2.cpp:20: note: synthesized method ‘LogUnit::LogUnit(const LogUnit&)’ first required here


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2007-09-01, 02:01 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *