LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-17-2011, 06:20 AM   #31
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896

Thanks John, once again, for all the effort you've put in.

Quote:
Originally Posted by johnsfine View Post
Can you describe the desired sequence of events from the point of view of the callback system?
No, but I can describe them from "my" viewpoint:

Originally in our software we had used virtual functions for passing the messages to the derived classes. There is a function X, in the base class which receives the messages, handles the common messages then and there itself, but there are some derived class specific messages which need to be handled by the derived class only. For those messages we had placed a virtual function call at the end of the function X, i.e. the messages which are not parsed by the base class should be passed to the derived class by calling that virtual function.

Now that virtual function 'call' is to be replaced by a code which calls the derived class specific functions.

It is not necessary to push in the derived class functions through the constructor of the derived class, it can be done through main() too.

It is not necessary that the owner of the callback vector ought to be a baseclass of AClass, but base class has to call those functions of the derived class through function X as described above.

Quote:
Originally Posted by johnsfine View Post
But meanwhile, I wrote some code for you that demonstrates a simpler approach to what I think you're trying to accomplish
I read the templates from the thinking in c++ but still ...
anyways I studied your code in detail which you had made very simple and I modified it as follows.
See RED:
Code:
#include <iostream>
#include <vector>

typedef int ResultType;
typedef int ParameterType;

class BaseCallBack 
{
     public:
	  virtual ~BaseCallBack () {}
	  virtual ResultType execute (ParameterType) = 0;
};

template <class Object> class CallBack : public BaseCallBack 
{
     public:
	  typedef ResultType (Object::* Fun) (ParameterType);
	  
	  Object& m_object;
	  Fun         m_fun;

	  CallBack   (Object& object, Fun fun) : m_object (object), m_fun (fun) {}
	  ~CallBack () {}
	  
	  ResultType execute (ParameterType p) 
	  {
	       return (m_object.*m_fun) (p);
	  }
};

class CallBackManager
{
     public:
	  static std :: vector<BaseCallBack*> call_backs;
	  
	  template <class A, class F> static void add (A& a, F f) 
	  {
	       call_backs.push_back (new CallBack<A> (a, f));
	  }
	  
	  static ResultType execute_next (ParameterType p)
	  {
	       if (call_backs.empty () == false)
	       {
		    ResultType result = call_backs[0]->execute( p );
		    delete call_backs [0];
		    
		    call_backs.erase (call_backs.begin ());
		    
		    return result;
	       }
	  }
};

std :: vector<BaseCallBack*> CallBackManager :: call_backs;

class innocentBaseClass
{
     public:
	  innocentBaseClass () {};
	  
	  void callExecute ()
	  {
		CallBackManager :: execute_next (11);
	  }
};

class AClass : public innocentBaseClass
{
     public:
	  ResultType fred (ParameterType p)
	  {
	       std::cout << "\nAClass::fred was called with parameter " << p << std::endl;
	       return 1;
	  }
};

int main()
{
    AClass a;

    CallBackManager :: add (a, &AClass :: fred);
   
    innocentBaseClass d1;
    d1.callExecute();

    return 0;
}
That code is working very properly now, but there are some unanswered questions:
1. The statements in GREEN, I couldn't understand why the two of them were there? How are they working? the internal vector declaration is understandable, but the external one?

2. Aren't we allowed to inherit the template classes? Are there any complications in it because of which it should be avoided?

I tried this:
Code:
#include <iostream>
#include <vector>

template <class objDerivedClass, typename functionParameterType, typename functionReturnType> class baseClass
{
     public:
	  typedef functionReturnType (objDerivedClass :: *functionName) (functionParameterType);
	  
	  objDerivedClass *c_classObj;
	  functionName     c_derivedClassFunctionName;
	  
	  baseClass (objDerivedClass *tempObj, functionName  tempFunctionName)
	  {
	       c_classObj                                 = tempObj;
	       c_derivedClassFunctionName = tempFunctionName;
	  }
	  	  
	  functionReturnType execute (functionParameterType p) 
	  {
	       return (c_classObj.*c_derivedClassFunctionName) (p);
	  }	  
};

class d : public template <class objDerivedClass, typename functionParameterType, typename functionReturnType> class baseClass
{
     static std :: vector<baseClass*> call_backs;
};

int main ()
{
     return 0;
}
The compilation error which I couldn't understand, I tried removing the keyword template, it didn't help.
Code:
anisha@linux-uitj:~> g++ john1.cpp
john1.cpp:24: error: expected class-name before ‘template’
john1.cpp:24: error: expected ‘{’ before ‘template’
john1.cpp:24: error: expected unqualified-id before ‘template’

Last edited by TheIndependentAquarius; 03-17-2011 at 06:21 AM.
 
Old 03-17-2011, 06:25 AM   #32
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Quote:
Originally Posted by SigTerm View Post
I'm not good at explaining, so you'll have to take this apart
It is nice to see you posting outside windows vs Linux thread Thanks for bothering, I am not yet too comfortable with the syntax of templates, I will see your code as soon as something gets working.

Last edited by TheIndependentAquarius; 03-17-2011 at 06:29 AM.
 
Old 03-17-2011, 07:31 AM   #33
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Anisha Kaul View Post
Code:
class d : public template <class objDerivedClass, typename functionParameterType, typename
Wrong syntax.
It doesn't work this way. It is either "class XYZ: public TemplateClass<Arguments>" or "template<Parameters> class XYZ: public TemplateClass<Parameters>"
Example:
Code:
template<typename Type> class Template{
public:
	Type variable;
};

class Derived1: public Template<int>{
};

template<typename Type> class Derived2: public Template<Type>{
public:
	Type anotherVariable;
};

class Derived3: public Template<int>{
	template <typename T> void convertVarToType(T& out){
		out = (T)variable;
	}
};

int main(int argc, char** argv){
	Derived1 obj1;
	Derived2<float> obj2;
	Derived3 obj3;
	return 0;
}

Last edited by SigTerm; 03-17-2011 at 09:21 AM.
 
1 members found this post helpful.
Old 03-17-2011, 07:55 AM   #34
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
Quote:
Originally Posted by Anisha Kaul View Post
For those messages we had placed a virtual function call at the end of the function X, i.e. the messages which are not parsed by the base class should be passed to the derived class by calling that virtual function.

Now that virtual function 'call' is to be replaced by a code which calls the derived class specific functions.
1) You're not saying why that virtual function ought to be replaced.

2) So far as I can tell, you never described how the vector of callbacks fits in. You never confirmed nor contradicted my guess that you want one such vector for the entire process.

Quote:
It is not necessary to push in the derived class functions through the constructor of the derived class, it can be done through main() too.
I never said you couldn't push elements into that vector in the derived class constructor. It just seemed like a strange thing to want to do. If you recall, your problem in that code was that the vector itself was a non static member of the base class, so there was a separate copy of the vector for every object of the derived type, and you were looking at the contents of each such vector before it was able to have any.

Quote:
It is not necessary that the owner of the callback vector ought to be a baseclass of AClass, but base class has to call those functions of the derived class through function X as described above.
In my sample code, I chose to make the vector a static data member of a class. That is one of the ways to make an item of data exist only once in the whole process (which is still only my guess at your intent).

I made the class containing that static data and associated static function not be a base class of AClass, only because I didn't understand any reason why it should be a base class of AClass. But I also have no reason why it shouldn't. If you want it to be a base class, you can.

Quote:
See RED:
Valid concept, but wrong syntax. Maybe later I'll test compile a correction to show you.

Edit: I typed this post so slowly that SigTerm answered that part while I was typing.

Quote:
1. The statements in GREEN, I couldn't understand why the two of them were there? How are they working? the internal vector declaration is understandable, but the external one?
That is one of the flaws in C++ as a language. You cannot define a class static data member inside the class definition.

You must declare the class static data member inside the class definition (typically in an hpp file included by every compilation unit needing that class definition). You must define the class static data member in exactly on compilation unit and outside the class definition. So in a multi-file project, the declaration and definition of class static data is typically in different files, which is an ugly language flaw.

Quote:
Aren't we allowed to inherit the template classes? Are there any complications in it because of which it should be avoided?
Yes, you can inherit from a template. You just had the syntax wrong. Yes, there are some very strange complications when inheriting from a template. But that doesn't mean avoid it. That just means understand the complications (which are unrelated to your syntax problem and not yet part of your code) and know how to work around them.

Last edited by johnsfine; 03-17-2011 at 07:57 AM.
 
1 members found this post helpful.
Old 03-18-2011, 03:38 AM   #35
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Quote:
Originally Posted by SigTerm View Post
Wrong syntax.
Example:
Code:
template<typename Type> class Template{
public:
	Type variable;
};

class Derived1: public Template<int>{
};
Thanks for helping.

That code may compile if you use a built in type "int", what if its like this?
Code:
#include <iostream>
#include <vector>

template <class objDerivedClass, typename functionParameterType, typename functionReturnType> class baseClass
{
     public:
};

class d : public baseClass <objDerivedClass, functionParameterType, functionReturnType> 
{
     public:
};

int main () { return 0; }
Error:
Code:
anisha@linux-uitj:~> g++ john1.cpp
john1.cpp:25: error: ‘objDerivedClass’ was not declared in this scope
john1.cpp:25: error: ‘functionParameterType’ was not declared in this scope
john1.cpp:25: error: ‘functionReturnType’ was not declared in this scope
john1.cpp:25: error: template argument 1 is invalid
john1.cpp:25: error: template argument 2 is invalid
john1.cpp:25: error: template argument 3 is invalid
I know I am having an understanding issue, but all the sites on the net I have found till now show "simple" examples, if there is something which demonstrates the "complicated" examples of templates, then please enlighten.

Last edited by TheIndependentAquarius; 03-18-2011 at 03:40 AM.
 
Old 03-18-2011, 05:08 AM   #36
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Quote:
Originally Posted by johnsfine View Post
1) Define a base class for all callback objects, that declares a pure virtual execute function and a virtual destructor.
Virtual destructor is needed when you want to new an object of the base class with the derived class memory and while deleteing it, you want derived classes destructor to be called first, AFAIK.

That might be needed when you want to put extra instructions in the destructor, is it required in our case?
 
Old 03-18-2011, 07:16 AM   #37
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Quote:
Originally Posted by johnsfine View Post
1) You're not saying why that virtual function ought to be replaced.
Thanks again, John, for following up.

Example: A derived class has the capability to handle 15 particular messages, but the task in question requires only 3 of them, so when the base class passes the messages to the derived class, because the derived class has one virtual function (declared and called from base class) to handle ALL the messages, it will have to go through all the if else statements in that function to match the message code.

That behavior is not clean, just to maintain the cleanness standards, that VF is to be replaced by the Functors.

Quote:
Originally Posted by johnsfine View Post
2) So far as I can tell, you never described how the vector of callbacks fits in. You never confirmed nor contradicted my guess that you want one such vector for the entire process.
Sorry for missing that point, I don't know any other way which can avoid the usage of vectors. The base class is supposed to "hold" all the desired functions of the derived class (acting as callback functions) in order to call a particular one when its associated message is received. To "hold" it needs a "container" which can be a vector!

Quote:
Originally Posted by johnsfine View Post
If you recall, your problem in that code was that the vector itself was a non static member of the base class, so there was a separate copy of the vector for every object of the derived type, and you were looking at the contents of each such vector before it was able to have any.
Yes I remember that, is that the reason you made a separate class for "pushing" the data in the vectors? I was about to ask that.

Quote:
Originally Posted by johnsfine View Post
I made the class containing that static data and associated static function not be a base class of AClass, only because I didn't understand any reason why it should be a base class of AClass.
I hope I made it clear why the base class has to "call" the derived class specific functions in my previous post. The RED part of the first code block in post 31, shows the base class calling the those DC functions.

Quote:
Originally Posted by johnsfine View Post
You must declare the class static data member inside the class definition (typically in an hpp file included by every compilation unit needing that class definition). You must define the class static data member in exactly on compilation unit and outside the class definition.
Sorry for being dumb, but the syntax of the vector declaration/definition in and out of the class does NOT appear to be different for me, I'll read up on that.
 
Old 03-18-2011, 08:15 AM   #38
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Anisha Kaul View Post
Thanks for helping.

That code may compile if you use a built in type "int", what if its like this?
Won't work.
This:
Code:
template <class objDerivedClass, typename functionParameterType, typename functionReturnType> class baseClass
{
     public:
};
is a template declaration - objDerivedClass, functionParameterType, functionReturnType are names used within template.
This template is "flexible".

However, here:
Code:
class d : public baseClass <objDerivedClass, functionParameterType, functionReturnType> 
{
     public:
};
you ask compiler for specialized instance of template that uses types objDerivedClass, functionParameterType, functionReturnType, which were not declared in example. To make it compile, you'll need to provide known/existing types for template parameters.

To use template as a base of non-template class, you need to provide all template parameters - and they should be existing types or "specialized" templates. You could do, for example:
Code:
class d : public baseClass <std::vector<int>, std::list<float>, std::map<float>> 
{
     public:
};
If you can't provide all template parameters, then derived class will have to be template itself. Fir example:
Code:
template<typename T> class D : public baseClass <std::vector<T>, std::list<T>, std::map<T>> 
{
     public:
};

class D2: public D<int>{
};
Think of template as if it were a macros. Template parameters used in template declaration do not really "exist" outside of template. Instead of them compiler substitutes types you provided for this particular instance. Template by itself does not exist - it won't be even compiled until you request a template instance that uses particular type.

In short, this:
Code:
template<typename T> class Template{
    T var;
};
is a template declaration. T does not have to exist - when you decide to use "Template", you'll give compiler a type to be instead of "T". For some template functions(not classes) compiler can guess types without your help, that's how std::swap and std::min works.

and this:
Code:
//Template declaration omitted
struct Struct{
    int a;
};

class Derived: public Template<Struct>{//<-- this
};

int main(int argc, char** argv){
    Template<int> instance;
    Derived d;
    return 0;
}
Is a request for template instance. "Derived" won't compile unlesss compiler knows of Struct type provided as parameter.

Quote:
Originally Posted by Anisha Kaul View Post
To "hold" it needs a "container" which can be a vector!
Or std::list, std::deque, std::queue, std::stack .
Consider what you want from the container, instead of picking vector blindly.
std::vector works best as an array replacement - initialize once, never resize, random item access. If you need anything else, you might want to try different container.

Quote:
Originally Posted by Anisha Kaul View Post
I know I am having an understanding issue, but all the sites on the net I have found till now show "simple" examples, if there is something which demonstrates the "complicated" examples of templates, then please enlighten.
Well, wikipedia has "template metaprogramming" article, but at this point it will simply confuse you. Also, I think Bjarne Stroustroup's books had some template examples, but I'm not sure about it - read them long time ago, plus the book is a heavy reading (was worth it, though). At this point you need to find any examples and keep reading them until it "clicks" and you get "aha" moment, or keep playing around with templates (with something SIMPLER - reduce number of template parameters, for a start) until you understand it. I'm not sure if there's any other way.

Last edited by SigTerm; 03-18-2011 at 08:29 AM.
 
1 members found this post helpful.
Old 03-18-2011, 01:39 PM   #39
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
I think I finally understand the vector is a dispatch table to avoid an if/else chain in dispatching by message type.

A switch statement is nearly as good as a dispatch table, so the comparison to an if/else chain is a bit unfair.

I still see no clear statement of how many vectors you ought to have:
1 per process
1 per base class
1 per derived class
1 per base or derived class object.

I'm think you don't want one per base or derived class object. I included that probably bad choice to help you think through what you really want.

You need to understand the difference between static data members of a class and ordinary data members. An ordinary data member exists once per object. Much of what you seem to be trying seems to be based on the idea that ordinary data members of a base class are shared by multiple object of the derived class. That is just not true. Every object of derived class includes its own complete copy of a base class object.

Quote:
Originally Posted by Anisha Kaul View Post
Example: A derived class has the capability to handle 15 particular messages, but the task in question requires only 3 of them, so when the base class passes the messages to the derived class, because the derived class has one virtual function (declared and called from base class) to handle ALL the messages, it will have to go through all the if else statements in that function to match the message code.
That usage would be simplest if the dispatch vector were a static member of the derived class. So the dispatch sequence is:
1) Some caller invokes a pure virtual execute function in the base class.
2) That call reaches a virtual override in the derived class.
3) That virtual override uses a static dispatch vector to reach desired member function.

But now I'm unclear on the behavior of the derived object pointers that are bound with the member function pointers.

When I thought your vector was some kind of queue for pending actions it made sense that each would bind an object pointer with a member function. But if it is more like a dispatch table than a pending action queue, then it doesn't seem right to bind in object pointers.

For each derived type, are you planning to support multiple objects? Or is each derived type intended to be a singleton?

Quote:
Sorry for being dumb, but the syntax of the vector declaration/definition in and out of the class does NOT appear to be different for me, I'll read up on that.
My point was that the language design requires that you both declare and define class static data members and that you do those in two different places.

Quote:
Originally Posted by johnsfine View Post
Apparently with someone else's help, you have used one of the strangest flexibilities of C++ templating: AClass derives from BaseClass but BaseClass is templated on AClass. That circular relationship is initially quite confusing. It is a very powerful technique in C++ programming. But I'm not convinced it is an appropriate technique for your problem.
Coming back to that idea from much earlier in the thread: I still think you need to think through your goals, especially regarding the distinction between a class and an object of that class.

But after doing so, you may have good use for the strange trick of using the derived class as a template parameter of base class. In your case, a three level hierarchy:

class BaseClass contains the pure virtual execute function

template <class TrueClass> VirtClass : public BaseClass
contains the virtual override of the execute function and contains the dispatch vector.

class AClass : public VirtClass<AClass>
contains whatever is really unique to AClass.

Notice that VirtClass is not one class. It is one piece of source code expanded by the compiler into multiple classes. After compilation there is one VirtClass for each derived class (AClass etc.).

When I do a template loopback like this, I give the formal class a name such as TrueClass, to emphasize the fact that any object of type VirtClass<X> is really an object of type X. In more interesting uses of this loopback trick, the VirtClass code is filled with casts equivalent to (TrueClass*)(this) so that the compiler can act upon the programmer's knowledge of the true type of the object.

With this design, you could make the vector a ordinary member of VirtClass in order to have one vector per derived object. Or you could could make it a static member of VirtClass to have one vector per derived class. Notice that a static member is not one vector for all of VirtClass, because VirtClass is not one class. It is one per derived class.

static data members of templated classes can get a bit messy. So if the derived class were a singleton anyway, then an ordinary member is cleaner than a static member.

Compared to the design at the top of this post, the compiled result would be nearly identical. The difference is in concentrating the source code. Everything in VirtClass is duplicated at compile time separately into each derived class, but exists in source level only once.

In either of these designs, all the callback complications discussed earlier vanish, because the full signature of the callback is known at compile time.

Last edited by johnsfine; 03-18-2011 at 02:07 PM.
 
1 members found this post helpful.
Old 03-22-2011, 03:36 AM   #40
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Thank you, Sigterm for the detailed response.

The facts you mentioned w.r.t the inheritance problem of the templates are quite understandable. Those facts did cross my mind but yet I thought there must be some way of getting over them, but now I understand there is not.

Quote:
Originally Posted by SigTerm View Post
If you can't provide all template parameters, then derived class will have to be template itself....Think of template as if it were a macros. Template parameters used in template declaration do not really "exist" outside of template. Instead of them compiler substitutes types you provided for this particular instance. Template by itself does not exist - it won't be even compiled until you request a template instance that uses particular type.
I understand now. It is becoming clear.

Quote:
Originally Posted by SigTerm View Post
Or std::list, std::deque, std::queue, std::stack .
Consider what you want from the container, instead of picking vector blindly.
std::vector works best as an array replacement - initialize once, never resize, random item access. If you need anything else, you might want to try different container.
The queues and the stacks won't work, as the "picking out" a particular set of elements will be at random. In fact I am thinking of using maps, since there will be two elements i.e. message code and the corresponding: (function name and the derived class object). There is a key concept in the maps, like the message code can be the key and can be used to call out the function.

The Addison Wesley's C++ Templates book is proving to be a great help. It is written in an easy language.
 
Old 03-22-2011, 10:41 PM   #41
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Thanks again John for the detailed response.

Quote:
Originally Posted by johnsfine View Post
A switch statement is nearly as good as a dispatch table, so the comparison to an if/else chain is a bit unfair.
I would like to ask you, John, in such a situation which sounds better (even though both work) w.r.t performance as well as the coding standards: the callbacks or the virtual functions? My boss said that virtual functions are to be replaced by the callbacks for "cleanness" sake.

Quote:
Originally Posted by johnsfine View Post
I still see no clear statement of how many vectors you ought to have:
1 per process
1 per base class
1 per derived class
1 per base or derived class object.
Let me bring in the client-server "distraction" once again.
The server and all the client processes have one object each, so for all the client processes (which includes the base class and the the derived class) there will be ONE vector, I mean one vector per client process.

Quote:
Originally Posted by johnsfine View Post
You need to understand the difference between static data members of a class and ordinary data members. An ordinary data member exists once per object. Much of what you seem to be trying seems to be based on the idea that ordinary data members of a base class are shared by multiple object of the derived class. That is just not true. Every object of derived class includes its own complete copy of a base class object.
Thanks for explanation, but I don't know which design of the above code made you think so, AFA these callbacks are concerned, there will be one object and the vector will be attached to that object only. (I don't think it makes sense to have multiple objects here in any way)

Quote:
Originally Posted by johnsfine View Post
That usage would be simplest if the dispatch vector were a static member of the derived class. So the dispatch sequence is:
1) Some caller invokes a pure virtual execute function in the base class.
2) That call reaches a virtual override in the derived class.
3) That virtual override uses a static dispatch vector to reach desired member function.
I would first like to know what is the proper way of implementing this scenario: the callbacks or the virtual functions? Your above said method uses again the virtual functions though the pure ones this time and secondly the further clients (which do more work than just sending and receiving the messages) are to be developed by the users of this software, we (I and my teammates) do think that it is better to lighten the job of the end users as much as we can, all possible work should be handled by the base class so the end user doesn't have to bother too much.

Quote:
Originally Posted by johnsfine View Post
My point was that the language design requires that you both declare and define class static data members and that you do those in two different places.
Okay, thanks, I'll read up more.

Quote:
Originally Posted by johnsfine View Post
But after doing so, you may have good use for the strange trick of using the derived class as a template parameter of base class. In your case, a three level hierarchy
I am thankful to you for the efforts you made for showing that design, but if we clarify the above matters then we may decide whether your said design should be followed or not.
 
Old 03-23-2011, 12:25 AM   #42
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Anisha Kaul View Post
w.r.t performance as well as the coding standards: the callbacks or the virtual functions?
If you worry about performance, you should run your code through profiler and search for bottlenecks.
Guessing "what'll be faster" will only make you waste time, and premature optimization might make your code harder to read, which will cost a LOT during maintenance. Besides, virtual method calls aren't the slowest - dynamic memory allocation/deallocation takes much more time.

Quote:
Originally Posted by Anisha Kaul View Post
My boss said
If you were hired to work on his/her project and that was an order you could write the request down (document it, in case of argument later), do exactly as he/she said, and if something goes wrong later (because of boss's decision), charge extra for fixing the problem. Or you could discuss it with your boss.
In any case, documenting feature requests is a good practice - saves a lot of time later.
 
Old 03-23-2011, 04:02 AM   #43
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,622
Blog Entries: 29

Original Poster
Rep: Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896Reputation: 896
Quote:
Originally Posted by SigTerm View Post
If you worry about performance, you should run your code through profiler and search for bottlenecks. Guessing "what'll be faster" will only make you waste time, and premature optimization might make your code harder to read, which will cost a LOT during maintenance. Besides, virtual method calls aren't the slowest - dynamic memory allocation/deallocation takes much more time.
I am actually not worrying about performance or any other thing. I asked the question because I wanted to get the object oriented concepts clear, I wanted to understand "Why", rather than just doing because I am asked to.

Last edited by TheIndependentAquarius; 03-23-2011 at 04:03 AM.
 
  


Reply

Tags
c++, template, vector


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Tropic of Vector – a blog devoted to Vector Linux Light, plus the Vector Linux LXer Syndicated Linux News 0 09-17-2009 01:30 PM
c++ vector ::size() nonzero before putting anything in the vector - ?! map250r Programming 21 01-10-2009 02:27 PM
LXer: Vector Linux 5.9-Pseudo64-0.1 -- Finally, 64-bit Vector LXer Syndicated Linux News 0 09-12-2007 01:20 PM
can i modify int 80 vector to a user-defined vector qqrilxk Linux - Security 1 03-03-2005 08:46 PM
Miseries at install from HDD&floppies, with vector ISO& how to boot with vector linux prctom VectorLinux 9 06-29-2004 05:27 AM


All times are GMT -5. The time now is 10:03 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration