Search Results

Search found 16 results on 1 pages for 'user231536'.

Page 1/1 | 1 

  • C++ design question on traversing binary trees

    - by user231536
    I have a binary tree T which I would like to copy to another tree. Suppose I have a visit method that gets evaluated at every node: struct visit { virtual void operator() (node* n)=0; }; and I have a visitor algorithm void visitor(node* t, visit& v) { //do a preorder traversal using stack or recursion if (!t) return; v(t); visitor(t->left, v); visitor(t->right, v); } I have 2 questions: I settled on using the functor based approach because I see that boost graph does this (vertex visitors). Also I tend to repeat the same code to traverse the tree and do different things at each node. Is this a good design to get rid of duplicated code? What other alternative designs are there? How do I use this to create a new binary tree from an existing one? I can keep a stack on the visit functor if I want, but it gets tied to the algorithm in visitor. How would I incorporate postorder traversals here ? Another functor class?

    Read the article

  • Google C++ style guide

    - by user231536
    I am curious as to what people think of the google C++ style guide. http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml I see references to it scattered across stackoverflow and would like to collect your opinions as to what you think about it.

    Read the article

  • C++ include header conventions

    - by user231536
    Suppose I have a file X.h which defines a class X, whose methods are implemented in X.cc. The file X.h includes a file Y.h because it needs Y to define class X. In X.cc, we can refer to Y because X.h has already included Y.h. Should I still include Y.h in X.cc ? I understand that I don't need to and I can depend on header guards to prevent multiple inclusions. But on the one hand, including Y.h makes X.cc a little more independent of X.h (can't be completely independent of course). What is the accepted practice? Another example: including <iostream> in both .h and .cc files. I see some people do this and some don't.

    Read the article

  • C++ refactor common code with one different statement

    - by user231536
    I have two methods f(vector<int>& x, ....) and g(DBConn& x, ....) where the (....) parameters are all identical. The code inside the two methods are completely identical except for one statement where we do different actions based on the type of x: in f(): we do x.push_back(i) in g(): we do x.DeleteRow(i) What is the simplest way to extract the common code into one method and yet have the two different statements? I am thinking of having a templated functor that overloads operator () (int a) but that seems overkill.

    Read the article

  • C++ multiple definition error

    - by user231536
    Starting with sth's answer to this question: http://stackoverflow.com/questions/3023760/c-template-specialization I was wondering how to resolve multiple definition errors if the following code is put in a header file included multiple times by different .cc files and linked together: template <typename T> class C { static const int K; static ostream& print(ostream& os, const T& t) { return os << t;} }; // general case template <typename T> const int C<T>::K = 1; // specialization template <> const int C<int>::K = 2;

    Read the article

  • C++ design with static methods

    - by user231536
    I would like to define as class X with a static method: class X { static string get_type () {return "X";} //other virtual methods } I would like to force classes which inherit from X to redefine the get_type() method and return strings different from "X" (I am happy if they just redefine get_type for now). How do I do this? I know that I cannot have virtual static methods. Edit: The question is not about the type_id, but in general about a static method that should be overriden. For example class X { static int getid() {return 1;} }

    Read the article

  • C++ compiler error on template specialization

    - by user231536
    I would like to specialize a template method for a class C that is itself templated by an int parameter. How do I do this? template <int D=1> class C { static std::string foo () { stringstream ss; ss << D << endl; return ss.str();} }; template <class X> void test() { cout << "This is a test" << endl;} template <> template <int D> void test<C<D> > () {cout << C<D>::foo() << endl;} The specialization for test() fails with "Too many template parameter lists in declaration of void test()".

    Read the article

  • [C++] Boost test: catch user defined exceptions

    - by user231536
    If I have user defined exceptions in my code, I can't get Boost test to consider them as failures. For example, BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MyTest,1) BOOST_AUTO_TEST_CASE(MyTest) { // code which throws user defined exception, not derived from std::exception. } I get a generic message: Caught exception: .... unknown location(0):.... It does not recognize this error as a failure since it is not a std::exception. So it does not honor the expected_failures clause. How do I enforce that the piece of code should always throw an exception? THis seems to be a useful thing to want. In case future code changes cause the code to pass and the exception is not thrown, I want to know that.

    Read the article

  • C++: need indexed set

    - by user231536
    I need an indexed associative container that operates as follows: initially empty, size=0. when I add a new element to it, it places it at index [size], very similar to a vector's push_back. It increments the size and returns the index of the newly added element. if the element already exists, it returns the index where it occurs. Set seems the ideal data structure for this but I don't see any thing like getting an index from a find operation. Find on a set returns an iterator to the element. Will taking the difference with set.begin() be the correct thing to do in this situation?

    Read the article

  • C++ Design Question on template types

    - by user231536
    I have a templated class template <typename T> class MyContainerClass For types to be substituted for T, it has to satisfy many requirements: for example, get_id(), int data(), etc. Obviously none of the fundamental types (PODs) are substitutable. One way I can provide this is via wrappers for the PODs that provide these functions. Is this an acceptable way? Another way would be to change the template to: template < typename T, typename C=traits<T> > class MyContainerClass and inside MyContainerClass, call traits::data() instead of data() on T objects. I will specialize traits<int>, traits<const char *> etc. Is this good design ? How do I design such a traits class (completely static methods or allow for inheritance) ? Or are the wrapper classes a good solution? What other alternatives are there?

    Read the article

  • C++ enforce conditions on inherited classes

    - by user231536
    I would like to define an abstract base class X and enforce the following: a) every concrete class Y that inherits from X define a constructor Y(int x) b) it should be possible to test whether two Y objects are equal. For a, one not very good solution is to put a pure virtual fromInt method in X which concrete class will have to define. But I cannot enforce construction. For b), I cannot seem to use a pure virtual method in X bool operator == (const X& other) const =0; because in overridden classes this remains undefined. It is not enough to define bool operator == (const Y& other) const { //stuff} because the types don't match. How do I solve these problems?

    Read the article

  • C++ how to make typedefs globally visible

    - by user231536
    I have a typedef typedef unsigned int my_type; used in a file. I would like to make it visible across all my files, without putting it in a header file included by everything. I don't want to go the header file route because as it stands this will be the only declaration in the header file (and it seems unnecessary to add a file just for this). Is there a way to do this? If instead I had: typedef X my_type; where X was a class, would I need to include X.h everywhere and have the typedef at the end of X.h ?

    Read the article

  • C++ write to front of file

    - by user231536
    I need to open a file as ofstream and write to the front of the file, while preserving the remaining contents of the file, which will be "moved". Similar to "prepend" a file. Is this possible using the STL or boost ?

    Read the article

  • C++ template specialization

    - by user231536
    I have a class template <typename T> class C { static const int K=1; static ostream& print(ostream& os, const T& t) { return os << t;} }; I would like to specialize C for int. //specialization for int template <> C<int>{ static const int K=2; } I want the default print method that works for int to remain and just change the constant. For some specializations, I want to keep K=1 and change the print method because there is no << operator. How do I do this?

    Read the article

  • C++ question on file formats and od

    - by user231536
    I have the following simple code: ofstream output("test"); output << 'a'; When I do an octal dump of the file, I get this: 0000000 000141 0000001 I can see that 000141 (in base 8) is 8 bits wide and 0000001 is probably EOF. What is the first byte of all 0's and why is it there? I know it is null is ascii but what is its purpose?

    Read the article

  • In C++, how can I make typedefs visible to every file in my project?

    - by user231536
    I have a typedef typedef unsigned int my_type; used in a file. I would like to make it visible across all my files, without putting it in a header file included by everything. I don't want to go the header file route because as it stands this will be the only declaration in the header file (and it seems unnecessary to add a file just for this). Is there a way to do this? If instead I had: typedef X my_type; where X was a class, would I need to include X.h everywhere and have the typedef at the end of X.h ?

    Read the article

1