Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

programci_84

Member Since 23 Feb 2009
Offline Last Active Today, 06:07 AM

Topics I've Started

[C++] Custom containers vs STL

01 March 2012 - 12:15 AM

Hi all,

I want to write my own container classes and I've some questions.

I've learnt that when we want to add one or more elements to an STL container (e.g. std::vector), it increases the size of the buffer more than we want. For example, if we have a 4-element-vector and if it's capacity is 4, when we want to insert 3 elements somewhere in it, it sets the capacity (or size, whatever you say) more than 7 (e.g. 10). I think this is done to avoid calling ::operator new frequently. But the size increases unnecessarily, right?

So, for an STL-like custom container, which one is more expensive?:
a) Using new/delete every time we want to add/delete elements (To keep the size of the buffer exact: for an n-element-buffer, the size is n*sizeof(element_type) ).
b) Increasing the capacity unnecessarily (To avoid calling ::operator new and ::operator delete every time).

btw, I've heard about "heap compaction" but I don't have any idea about that.

Hope I could describe.

Thx in advance.
-R

A complex inheritance problem

22 October 2011 - 09:25 AM

Hi all,

I can describe my problem only throug a sample code. Sorry for that.

I've a class inherited through IUnknown (and I want to hide some members):

//a1.h
#define _INTERFACE(IName) interface DECLSPEC_NOVTABLE IName : public IUnknown
#define _INTERFACE_(IName, IBase) interface DECLSPEC_NOVTABLE IName : public IBase

_INTERFACE(A1)
{
      //AddRef(), Release(), QueryInterface() come here!

  	void A1_Func(float y);
};
////////// end of a1.h /////////////

//a1.cpp
struct A1_D : public A1
{
  	/*  AddRef(), Release() and QueryInterface() definitons come here!  */
  
 	float x;
  	
 	ULONG _ref_count
};

inline			A1_D* CImpl (A1* ptr)		{ return (		A1*)ptr; }
inline	const	A1_D* CImpl (const A1* ptr)	{ return (const A1*)ptr; }

void A1_Func(float y)
{
 	A1_D* pA1_D = CImpl(this);
 	pA1_D->x = y;
}
////////// end of a1.cpp /////////////

Now I want a class inherited through A1:

//a2.h
_INTERFACE_(A2, A1)
{
 	void A2_Func();
};
////////// end of a2.h /////////////

//a2.cpp
struct A2_D : public A2
{
 	/* AddRef() etc. definitons come here! */

	
};

It seems that A2::A1_Func() does not work properly.

How can I do this?

Hope I could explain.

Thanks in advance.
-R

PARTNERS