T
tridion
Could someone answer the following questions for me :
1. Discuss this C++ code fragment. Do you see any potential problems ?
BSTR bsValue = _bstr_t("a string");
pComInterface->DoSomething(bsValue);
2. Give examples of the problems inherent in using the C preprocessor. When
might you want to use it ?
3. What are the errors in this C++ code fragment ? Consider both errors that
would prevent succesful compilation and errors that would lead to unexpected
behaviour when using these classes.
class base
{
base() {m_pData = new Data[100];}
~base() { delete m_pData;}
private:
Data* m_pData;
}
class derived : public base
{
public:
derived() {m_iCount = 7; m_pDerivedData = new Data();}
~derived() { delete m_pDerivedData;}
private:
int m_iCount;
Data* m_pDerivedData;
};
4. Discuss how you might improve this C++ code
bool LessThan(Thing* pLeft, Thing* pRight)
{
// body of comparison function
}
typedef bool (*LessThanFnPtr)(Thing* pLeft, Thing* pRight);
void Sort(Thing* pArray, int iSize, LessThanFnPtr pLessThan)
{
// bubble sort code using pLessThan to compare things
}
void DoSorting()
{
int iSize = 1000;
Thing* pData = new Thing[iSize];
// read in data
Sort(pData, iSize, &LessThan);
// use sorted data
}
5. How would you improve the design of this C++ code? Point out any
problems and the causes, and suggest or show a remedy.(Hint: errors)
void SomeClass::SetValues()
{
Calculator* pCalculator = new Calculator();
m_iVal1 = pCalculator->DoCalcSomething(1, 2);
m_iVal2 = pCalculator->DoAnotherThing(m_iVal1);
delete pCalculator;
}
6. The following C++ code is correct, but it's considered to be bad style.
Discuss.
// Somewhere we have
bool bCondition(false);
// Somewhere else, perhaps immediately after...
int iValue;
if (bCondition)
iValue = 1;
else
iValue = 2;
1. Discuss this C++ code fragment. Do you see any potential problems ?
BSTR bsValue = _bstr_t("a string");
pComInterface->DoSomething(bsValue);
2. Give examples of the problems inherent in using the C preprocessor. When
might you want to use it ?
3. What are the errors in this C++ code fragment ? Consider both errors that
would prevent succesful compilation and errors that would lead to unexpected
behaviour when using these classes.
class base
{
base() {m_pData = new Data[100];}
~base() { delete m_pData;}
private:
Data* m_pData;
}
class derived : public base
{
public:
derived() {m_iCount = 7; m_pDerivedData = new Data();}
~derived() { delete m_pDerivedData;}
private:
int m_iCount;
Data* m_pDerivedData;
};
4. Discuss how you might improve this C++ code
bool LessThan(Thing* pLeft, Thing* pRight)
{
// body of comparison function
}
typedef bool (*LessThanFnPtr)(Thing* pLeft, Thing* pRight);
void Sort(Thing* pArray, int iSize, LessThanFnPtr pLessThan)
{
// bubble sort code using pLessThan to compare things
}
void DoSorting()
{
int iSize = 1000;
Thing* pData = new Thing[iSize];
// read in data
Sort(pData, iSize, &LessThan);
// use sorted data
}
5. How would you improve the design of this C++ code? Point out any
problems and the causes, and suggest or show a remedy.(Hint: errors)
void SomeClass::SetValues()
{
Calculator* pCalculator = new Calculator();
m_iVal1 = pCalculator->DoCalcSomething(1, 2);
m_iVal2 = pCalculator->DoAnotherThing(m_iVal1);
delete pCalculator;
}
6. The following C++ code is correct, but it's considered to be bad style.
Discuss.
// Somewhere we have
bool bCondition(false);
// Somewhere else, perhaps immediately after...
int iValue;
if (bCondition)
iValue = 1;
else
iValue = 2;