inheritance and casting

C

Christopher

I have a class A that wraps a C-Style class B, by inheriting from B
and adding one std::string member to which I initiziliza the char * in
B.

I made a vector of class As

Now I am trying to pass the vector of class As to a C-Style funtion
from the same API, which is asking for a B**
cfunction(&myvector[0], myvector.size());

I get an unhandled exception down in mem copy.

Do I have to cast the vector of class As to a vector of class Bs?
How do you cast the contents of a vector?
Or do I have to make a whole new vector of class Bs just to pass the
dern thing?

---------------------------------------
The function I am calling:

HRESULT CreateInputLayout(
const D3D10_INPUT_ELEMENT_DESC *pInputElementDescs,
UINT NumElements,
const void *pShaderBytecodeWithInputSignature,
SIZE_T BytecodeLength,
ID3D10InputLayout **ppInputLayout
);

-------------------------------------
How I am calling it:

m_device.CreateInputLayout(&inputElementDescs[0],
inputElementDescs.size(),
passDesc.pIAInputSignature,

passDesc.IAInputSignatureSize,
&inputLayout);

------------------------------------
Definition of the vector:

const std::vector<InputElementDescription> inputElementDescs

-----------------------------------

Definition of the InputElementDescription Class :


#ifndef INPUTELEMENTDESCRIPTION_H
#define INPUTELEMENTDESCRIPTION_H

// EngineX Includes
#include "Graphics\3D\Buffers.h"

// DirectX Includes
#include <d3d10.h>
#include <d3dx10.h>

// Standard Includes
#include <string>


//------------------------------------------------------------------------------------------
/**
* C++ Wrapper for a D3D Input Element Description
*
* The original structure did not control the lifetime of the semantic
name.
* Simply cast to a D3D10_INPUT_ELEMENT_DESCRIPTION when passing to the
D3D API.
**/
class InputElementDescription : public D3D10_INPUT_ELEMENT_DESC
{
public:

/**
*
**/
InputElementDescription(const std::string & semanticName,
const unsigned semanticIndex,
const DXGI_FORMAT format,
const unsigned inputSlot,
const unsigned alignedByteOffset,
const D3D10_INPUT_CLASSIFICATION
inputSlotClass,
const unsigned instanceDataStepRate);

/**
*
**/
InputElementDescription(const InputElementDescription & rhs);

/**
*
**/
~InputElementDescription();

/**
*
**/
const InputElementDescription & operator =(const
InputElementDescription & rhs);


/**
* Gets the semantic name as a string
**/
const std::string & GetSemanticName() const;

/**
*
**/
static void GetInputElementDesc
(std::vector<InputElementDescription> & inputElementDescs,
const BufferContentType
contentType,
const unsigned startIndex,
const unsigned inputSlot,
const bool perInstance = false);

private:

std::string m_semanticName;
};

#endif // INPUTELEMENTDESCRIPTION_H

----------------------------------------

Implementation of the InputElementDescription Class:



#include "InputElementDescription.h"



//------------------------------------------------------------------------------------------
InputElementDescription::InputElementDescription(const std::string &
semanticName,
const unsigned
semanticIndex,
const DXGI_FORMAT
format,
const unsigned
inputSlot,
const unsigned
alignedByteOffset,
const
D3D10_INPUT_CLASSIFICATION inputSlotClass,
const unsigned
instanceDataStepRate)
:
D3D10_INPUT_ELEMENT_DESC(),
m_semanticName(semanticName)
{
SemanticName = m_semanticName.c_str();
SemanticIndex = semanticIndex;
Format = format;
InputSlot = inputSlot;
AlignedByteOffset = alignedByteOffset;
InputSlotClass = inputSlotClass;
InstanceDataStepRate = instanceDataStepRate;
}

//------------------------------------------------------------------------------------------
InputElementDescription::InputElementDescription(const
InputElementDescription & rhs)
:
D3D10_INPUT_ELEMENT_DESC(),
m_semanticName(rhs.m_semanticName)
{
SemanticName = m_semanticName.c_str();
SemanticIndex = rhs.SemanticIndex;
Format = rhs.Format;
InputSlot = rhs.InputSlot;
AlignedByteOffset = rhs.AlignedByteOffset;
InputSlotClass = rhs.InputSlotClass;
InstanceDataStepRate = rhs.InstanceDataStepRate;
}

//------------------------------------------------------------------------------------------
InputElementDescription::~InputElementDescription()
{
}

//------------------------------------------------------------------------------------------
const InputElementDescription & InputElementDescription::eek:perator =
(const InputElementDescription & rhs)
{
if( &rhs == this )
{
return rhs;
}

m_semanticName = rhs.m_semanticName;

SemanticName = m_semanticName.c_str();
SemanticIndex = rhs.SemanticIndex;
Format = rhs.Format;
InputSlot = rhs.InputSlot;
AlignedByteOffset = rhs.AlignedByteOffset;
InputSlotClass = rhs.InputSlotClass;
InstanceDataStepRate = rhs.InstanceDataStepRate;

return *this;
}

//------------------------------------------------------------------------------------------
const std::string & InputElementDescription::GetSemanticName() const
{
return m_semanticName;
}

//------------------------------------------------------------------------------------------
void InputElementDescription::GetInputElementDesc
(std::vector<InputElementDescription> & inputElementDescs,
const
BufferContentType contentType,
const unsigned
startIndex,
const unsigned
inputSlot,
const bool
perInstance)
{
// Start with an empty vector
inputElementDescs.clear();

// All vertex buffers will be bound to one input slot each.
//
// A vertex buffer containing a component that fits into 16 bytes
(4 floats or 4 ints) or less will have one
// input element description.
//
// For those buffers with content types that are larger than 16
bytes:
//
// For every additional 16 bytes a content type takes up, an
additional input element description will have to be generated.
// D3D treats the next 16 bytes as an additional semantic index, as
if it was a seperate piece of data then the first 16 bytes.
// However, the entire component element will be assigned to the
desired vertex shader input.
//
// See the D3D10 Instancing Sample for reference on how a vertex
buffer contains a 4x4 float matrix and its corresponding
// input element description.

D3D10_INPUT_CLASSIFICATION inputSlotClass =
D3D10_INPUT_PER_VERTEX_DATA;
unsigned instanceDataStepRate = 0;

if( perInstance )
{
inputSlotClass = D3D10_INPUT_PER_INSTANCE_DATA;
instanceDataStepRate = 1;
}

DXGI_FORMAT format = GetFormat(contentType);
unsigned bytesPerElement = GetFormatSize(format);
unsigned dataSize = GetStride(contentType);

for(unsigned i = 0; i * bytesPerElement < dataSize; ++i)
{
InputElementDescription inputElementDesc
(ContentTypeToSemanticName(contentType),
startIndex + i,
GetFormat(contentType),
inputSlot,
i * bytesPerElement,
inputSlotClass,
instanceDataStepRate);

inputElementDescs.push_back(inputElementDesc);
}
}

-------------------------------------

Defintion of the class it wraps:

typedef struct D3D10_INPUT_ELEMENT_DESC {
LPCSTR SemanticName;
UINT SemanticIndex;
DXGI_FORMAT Format;
UINT InputSlot;
UINT AlignedByteOffset;
D3D10_INPUT_CLASSIFICATION InputSlotClass;
UINT InstanceDataStepRate;
} D3D10_INPUT_ELEMENT_DESC;
 
I

Ian Collins

Christopher said:
I have a class A that wraps a C-Style class B, by inheriting from B
and adding one std::string member to which I initiziliza the char * in
B.

I made a vector of class As

Now I am trying to pass the vector of class As to a C-Style funtion
from the same API, which is asking for a B**
cfunction(&myvector[0], myvector.size());
See the thread "array and polymorphism" from earlier today.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top