Home

On Sep 19, 2:29 pm, Barry <dhb2...@gmail.com> wrote:
> g.vuko...@gmail.com wrote:
> > Hi all,
>
> > I asking me how to design my method. Should I use pointer only? Or
> > references? Or Values? I don't know...
>
> > Below is a small method to read the mem amount of a machine. Fell free
> > to edit the src below
>
> > #include "MemSysInfo.h"
>
> > namespace SysInfo {
>
> > unsigned int * SysInfo::MemSysInfo_t::GetSize() const { // SHOULD I
> > RETURN A POINTER/REFERENCE/VALUE?
>
> > using namespace std;
> > using namespace boost;
>
> > ifstream* const inFile = new ifstream("/proc/meminfo"); // SHOULD
> > string* inLine = new string; // I
> > const string* const outLine = new string("\\2"); // USE
> > const regex* const rgx = new regex("^(MemTotal: )(\\d+)
> > ( kB)?"); // POINTER ONLY?
> > unsigned int* memSize = new unsigned int(0); // WHAT WOULD
> > stringstream* stringToInt = new stringstream; // YOU USE?
>
> > if(!inFile->is_open())
> > throw "can_not_open_proc_meminfo"; // SHOULD I DELETE POINTERS
> > BEFORE THROW?
> > else if(inFile == 0 || inLine == 0 || outLine == 0 || rgx == 0 ||
> > memSize == 0 || stringToInt == 0)
> > throw "no_memory_available"; // SHOULD I DELETE POINTERS BEFORE
> > THROW?
>
> > while(getline(*inFile, *inLine))
> > if(regex_match(*inLine, *rgx)) {
> > *stringToInt << regex_replace(*inLine, *rgx, *outLine);
> > *stringToInt >> *memSize;
> > break;
> > }
>
> > inFile->close();
>
> > delete inFile;
> > delete inLine;
> > delete outLine;
> > delete rgx;
> > delete stringToInt;
>
> > return(memSize);
> > }
>
> > } // namespace SysInfo
>
> > Goran
>
> I would say use all value in your case, have you try?
>
> --
> Thanks
> Barry

Yes, I tried it all. By value, by reference and by pointer. All
works... But why should I use values? When do I use values, pointers
or references? Are there rules I understand?

Goran

previous
next

Re: C++ Primer ex 7.5
Re: split a string of space separated substrings - elegant solution?
Re: How can I use python for file processing
Re: Python statements not forcing whitespace is messy?
Re: copying elements from a <list> to <deque>
Mimo Wszystko
Fundacja Iskierka
Mam Marzenie
Akogo
Fundacja Avalon