• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

COM/DCOM

Hi experts,
           
                        what is the use of ATL_NO_VTABLE macro what it actually does. Is it disable the vtable of the base classes or the current class.

Regards,
Vimal.
0
vimalalex
Asked:
vimalalex
1 Solution
 
Paul JacksonSoftware EngineerCommented:
0
 
Todd GerbertIT ConsultantCommented:
According to MSDN (http://msdn.microsoft.com/en-us/library/w4baz6ss(v=VS.80).aspx), ATL_NO_VTABLE expands to declspec(novtable), which causes the linker to eliminate the vtable (and all the functions it points to), and should only ever be applied to an abstract base class.

It disables the vtable of the class to which the modifier is applied, but you should not apply it to your most-derived (or any creatable) class.
0
 
vimalalexAuthor Commented:
Hi tgerbert,

                       By eliminating vtable so we cant use function overridding right if we have normal virtual function in base class.What is the necessary for eliminating vtable of base class.

regards,
vimal.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Deepu AbrahamR & D Engineering ManagerCommented:
Hi Vimal,

No, I guess this is only meant for the specific class. See the example below:

Animal* animalObj = new Animal();
animalObj->eat();//-------> this will fail

And MSFT clearly says that "Using declspec(novtable)in a class declaration prevents the vtable pointer from being initialized in the class constructor and destructor. When you build your project, the linker eliminates the vtable and all functions to which the vtable points. "

This also explains it pretty neatly
http://www.codeproject.com/Messages/2380416/Re-ATL_NO_VTABLE-means.aspx
class __declspec(novtable) Animal {
    public:
         virtual void eat() const { 
            std::cout << "I eat like a generic Animal." << std::endl; 
        }
        virtual ~Animal() { 
        }
};
 
class Wolf : public Animal {
    public:
        void eat() const { 
            std::cout << "I eat like a wolf!" << std::endl; 
        }
        virtual ~Wolf() { 
        }
};


	//Animal* animalObj = new Animal(); 
	//animalObj->eat();//-------> this will fail
	
	Animal* animalObj = new Wolf();
	animalObj->eat();//-------> this will pass

Open in new window

0
 
vimalalexAuthor Commented:
Hi DeepuAbrahamK,

                       Thanks for your excellent reply.
Regards,
Vimal.
0
 
Todd GerbertIT ConsultantCommented:
MSDN also says "You must use ATL_NO_VTABLE, and consequently declspec(novtable), with only base classes that are not directly creatable", so you shouldn't apply it to Animal in the above example because Animal doesn't contain any pure virtual functions (i.e. it can be directly created).

The point is that since you can't create an abstract base class anyway, there's no need for it to have a vtable so it can be removed to reduce code size.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now