Differences

This shows you the differences between two versions of the page.

Link to this comparison view

const [2022/04/16 12:22] (current)
Line 1: Line 1:
 +
 +<code>
 +
 + make -k main  && cat main.cpp && ./main 
 +g++     main.cpp   -o main
 +/* #ident "$Id: main.cpp$"
 + * @author: rzr@gna.org - rev: $Author: rzr$
 + * Copyright: See README file that comes with this distribution
 + *****************************************************************************/
 +
 +#include <cstdio>
 +#include <cstdlib>
 +#include <cstring>
 +#include <iostream>
 +#include <cassert>
 +
 +#define LOG()                                                           \
 +  std::cout                                                             \
 +  << __FILE__ << ":" << __LINE__ << ": log: " << __PRETTY_FUNCTION__ << std::endl
 +
 +class Main
 +{
 +public:
 +
 +  Main() {
 +    LOG();
 +    ptr_ = new char[1];
 +  }
 +
 +  virtual ~Main() {
 +    LOG();
 +    delete( ptr_);
 +  }
 +
 +public:
 +
 +  inline char * getPtr() const {
 +    LOG();
 +    return(getPtrNew());
 +  }
 +
 +  ///TODO: express: char const * const getPtr() const
 +  inline  char const * const getPtr(bool isconst/*=true*/) const {
 +    LOG();
 +    assert(isconst == true);
 +    return( getPtrConst() );
 +  }
 +
 +public: //or private as you want
 +
 +  char * getPtrNew()  const {
 +    LOG();
 +    return(strdup(ptr_));
 +  }
 +
 +  char const * const getPtrConst() const
 +  {
 +    LOG();
 +    return(ptr_);
 +  }
 +
 +private:
 +  char * ptr_;
 +
 +};
 +
 +
 +int main()
 +{
 +  Main const main;
 +  {
 +    LOG();
 +    char* pnew = main.getPtr();
 +    delete(pnew);
 +  }
 +  {
 +    LOG();
 +    Main const main;
 +    char const * const pconst = main.getPtr(true);
 +    // no need to delete , main's destructor will do
 +  }
 +  {
 +    LOG();
 +    char const */* const*/ pconst = main.getPtr(true);
 +    pconst  = 0; // TODO: should be avoid if prev line use const..
 +    LOG();
 +    char const * const pnew = main.getPtr(); // TODO: delete const ?
 +    delete(pnew);
 +  }
 +  return 0;
 +}
 +
 +/*
 +
 +  make -k main && cat main.cpp && ./main 
 +
 +*/
 +
 +//#eof "$Id:$"
 +
 +main.cpp:21: log: Main::Main()
 +main.cpp:67: log: int main()
 +main.cpp:33: log: char* Main::getPtr() const
 +main.cpp:47: log: char* Main::getPtrNew() const
 +main.cpp:72: log: int main()
 +main.cpp:21: log: Main::Main()
 +main.cpp:39: log: const char* const Main::getPtr(bool) const
 +main.cpp:53: log: const char* const Main::getPtrConst() const
 +main.cpp:26: log: virtual Main::~Main()
 +main.cpp:78: log: int main()
 +main.cpp:39: log: const char* const Main::getPtr(bool) const
 +main.cpp:53: log: const char* const Main::getPtrConst() const
 +main.cpp:81: log: int main()
 +main.cpp:33: log: char* Main::getPtr() const
 +main.cpp:47: log: char* Main::getPtrNew() const
 +main.cpp:26: log: virtual Main::~Main()
 +
 +</code>
 +
 +
 +One track to avoid the cast :
 +
 +<code>
 + template<typename T>
 +  T getPtr() {
 +    return (0);
 +  }
 +</code>
 +
 +
 +==== MISC ====
 +
 +http://en.wikipedia.org/wiki/Const-correctness#Loopholes_to_const-correctness
 +
 +  error: assignment of read-only reference
 +
 +
  
const.txt ยท Last modified: 2022/04/16 12:22 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki