6 Replies - 361 Views - Last Post: 11 February 2014 - 11:57 PM Rate Topic: -----

#1 cphillippe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-October 13

classes

Posted 10 February 2014 - 01:22 PM

hello im trying to write a program that uses classes. now if i have 2 basic classes like so,
is this the proper way to use class Foo in class Boo? i know this works im just curious if their is a better way


class Foo
{
public:
  Foo();
  void init();
}


and then

class Boo
{
public
  Boo();
  void initboo();
private
  Foo f;
}

This post has been edited by JackOfAllTrades: 10 February 2014 - 03:04 PM
Reason for edit:: Added code tags

Is This A Good Question/Topic? 0
  • +

Replies To: classes

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: classes

Posted 10 February 2014 - 03:04 PM

It's *a* way. Whether it's the right way or not depends on the intended use of the classes.
Was This Post Helpful? 1
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: classes

Posted 10 February 2014 - 04:32 PM

You usually don't have a dedicated init function for each class. The initialization should happen in the constructor. If you define a init function to called within your constructor, be sure to make it private (or protected).
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,465
  • Joined: 05-May 12

Re: classes

Posted 10 February 2014 - 10:00 PM

Actually different people have different philosophies about constructors and init functions. I tend to subscribe to the idea that constructors should implement brain dead simple initialization that either has no chance of failing and throwing an exception, or if it does throw an exception, the object instance is in a good predictable state. If the constructor can fail or throw an exception then the class should have a public init function.

Basically, I hate code that looks like this:
class MyFileWrapper
{
public:
    MyFileWrapper(const std::string & name)
        : m_buffer(32768)
        , m_file(name)
        , m_index(0)
        , m_name(name)
    {
    }

    int getIndex() const
    {
        return m_index;
    }

    std::string getName() const
    {
        return m_name;
    }

private:
    std::vector<char> m_buffer;
    std::ifstream m_file;
    int m_index;
    std::string m_name;
};



If the vector fails to allocate the space, will the file be opened? If the file fails to open, will the m_index and m_name be initialized? If they are not initialized what is the expected result of getName()?
Was This Post Helpful? 0
  • +
  • -

#5 cphillippe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-October 13

Re: classes

Posted 10 February 2014 - 10:54 PM

well the functions are just examples for the class i was more wondering about using foo f in the boo class or would it be better to pass a class through function calls
like
void initboo(foo* f);


or just leave it private
class Boo
{
public
void initboo();
.
.
.
private
 foo* f;
}


This post has been edited by Skydiver: 11 February 2014 - 06:26 AM
Reason for edit:: Put code in code tags. Learn to do this yourself.

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,465
  • Joined: 05-May 12

Re: classes

Posted 11 February 2014 - 06:41 AM

I'm going to assume that the question you were asking was which one is better:
class Boo
{
public:
    Boo(Foo * foo);

private:
    Foo * m_foo;
}


or
class Boo
{
public:
    Boo();
    initboo(Foo * foo);

private:
    Foo * m_foo;
}



Again it really depends.

If the constructor or initboo() were trivial and cannot fail, then the former would be better because the code would be less verbose:
Boo::Boo(Foo * foo)
    : m_foo(foo)
{
}


or
Boo::initfoo(Foo * foo)
{
    m_foo = foo;
}



Now, if the constructor or initboo() were non-trivial and could throw exceptions, you've seen which camp I fall in my post above.
Boo::Boo(Foo * foo)
    : m_foo(foo)
{
    m_foo->DoSomeOperationWhichMayThrow();
}


or
Boo::initfoo(Foo * foo)
{
    m_foo = foo;
    m_foo->DoSomeOperationWhichMayThrow();
}


Was This Post Helpful? 0
  • +
  • -

#7 cphillippe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-October 13

Re: classes

Posted 11 February 2014 - 11:57 PM

yeah, thank you. figuring out how to ask it properly is not always easy
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1