Connecting Tech Pros Worldwide Forums | Help | Site Map

A Few C++ Questions

tridion
Guest
 
Posts: n/a
#1: Jul 23 '05
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;





Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: A Few C++ Questions


"tridion" <noemail@given.com> wrote...[color=blue]
> Could someone answer the following questions for me :[/color]

Homework is supposed to be done by the assignee, not by the newsgroup.
I'll start, you finish.
[color=blue]
> 1. Discuss this C++ code fragment. Do you see any potential problems ?
>
> BSTR bsValue = _bstr_t("a string");
> pComInterface->DoSomething(bsValue);[/color]

Of course I see potential problems. Code fragments are based on many
assumptions, and as we know, assumptions are the mother of all FUps.
[color=blue]
> 2. Give examples of the problems inherent in using the C preprocessor.
> When might you want to use it ?[/color]

#define NULL 0
[color=blue]
> 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];}[/color]


'Data' is undefined.
[color=blue]
> ~base() { delete m_pData;}[/color]

Wrong delete is used.
[color=blue]
> private:
> Data* m_pData;
> }[/color]

The "Rule of Three" is not followed. A semicolon is missing.
[color=blue]
> class derived : public base[/color]

Impossible to derive from 'base' since 'base' has both c-tor and d-tor
private.
[color=blue]
> {
> public:
> derived() {m_iCount = 7; m_pDerivedData = new Data();}[/color]


'Data' is still undefined.
[color=blue]
> ~derived() { delete m_pDerivedData;}
> private:
> int m_iCount;
> Data* m_pDerivedData;
> };[/color]

The "Rule of Three" is not followed here either.
[color=blue]
> 4. Discuss how you might improve this C++ code
> [...][/color]

V


Peter Koch Larsen
Guest
 
Posts: n/a
#3: Jul 23 '05

re: A Few C++ Questions


Ill continue where Victor stopped ;-)
"tridion" <noemail@given.com> skrev i en meddelelse
news:NNCdncGU2qQNp5PfRVnyiQ@eclipse.net.uk...[color=blue]
> Could someone answer the following questions for me :
>[/color]
[snip][color=blue]
>
> 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
> }[/color]

Use the standard library. Then this comes down to:

void DoSorting()
{
std::vector<Thing> data;

// read in data
std::sort(data.begin(),data.end()); // assuming Thing is comparable
and a "normal" sort is wanted.
}
[color=blue]
> 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;
> }[/color]

Do not use new here:
void SomeClass::SetValues()
{
Calculator pCalculator;
m_iVal1 = pCalculator.DoCalcSomething(1, 2);
m_iVal2 = pCalculator.DoAnotherThing(m_iVal1);
}

Note how the variable naming convention now gives the calculator object a
misleading name.
[color=blue]
>
> 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;[/color]

You could use iValue = bCondition?1:2;

but considering the horrible style found in these samples I'm not sure this
is what is meant.

/Peter


red floyd
Guest
 
Posts: n/a
#4: Jul 23 '05

re: A Few C++ Questions


tridion wrote:[color=blue]
> Could someone answer the following questions for me :
> [blatant "do my homework" request redacted][/color]

Could you give me your instructor's email address so I can send it
direct to him or her, saving you the trouble?
Mike Wahler
Guest
 
Posts: n/a
#5: Jul 23 '05

re: A Few C++ Questions


"Peter Koch Larsen" <pklspam@mailme.dk> wrote in message
news:V7sPd.99832$Vf.3882852@news000.worldonline.dk ...[color=blue][color=green]
> >
> > 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;[/color]
>
> You could use iValue = bCondition?1:2;
>
> but considering the horrible style found in these samples I'm not sure[/color]
this[color=blue]
> is what is meant.[/color]

In further pursuit of 'horrible', how about:

int iValue(-bCondition + 2);

:-)

-Mike


Closed Thread