Hey,

I'm developing a program in which there are two classes. The purpose of one of them is to create vectors that change dinamically (class A) and the other is a bunch of methods to operate on these vectors (class B).
The question is: I don't see any need to define a constructor in class B. In fact, I just define an object of class B, in main(), to access to the methods within it.
Is there any problem to not define a constructor, but using an object to just access to those methods?

Thanks

Recommended Answers

All 3 Replies

Is there any problem to not define a constructor, but using an object to just access to those methods?

No, if you don't provide a constructor the compiler will provide some basic ones for you.

If you're only going to declare a class so you can use methods in it to do things to some other class, you could consider making those methods static. That way, you don't even need an instance of the class to call the methods:

#include <iostream>

/// Some (really simple) class
class A
{
public :
    A( int x ) : m_x( x ) {}
    const int& x() const { return m_x; }

    int m_x;
};

/// Class with utility methods
class B
{
public : 
    static int Square( const A& a ){ return a.x() * a.x(); }
};

int main()
{
    A a(3)

    std::cout << "The square of " << a.x() << " is " << B::Square( a ) << std::endl;
    return 0;
}

Sincerely I don't understand the syntax you've used.
I'm a bit confused, and I don't know why I should use static members. The advantage is just to not have an instance of the class to call methods?
I'm newbie in C++, of course, so I think that all the things must be used with objects to access to the methods of a class, and so it would more intuitive to define objects of class B in main() than not to do it.

Sorry for confusing you! If you have time, you should search for "C++ static function" in your favourite search engine :o)

I'm newbie in C++, of course, so I think that all the things must be used with objects to access to the methods of a class, and so it would more intuitive to define objects of class B in main() than not to do it.

It may be more intuative to you, but it's not free. When you instantiate an object, then one needs to be created on the stack (or in the heap). This is something that takes time. Depending on your object, it can take a lot of time. In your case, it sounds like the object will not contain any data members (just functions), so the overhead will be minimal, but this might not true in the general case.

The syntax for declaring and using static functions isn't that complicated, you simply put the static keyword before the function when you declare it. When you want to use the function, then you call it like you would any other function, but you have to put the class-name and a :: before the function name. That's pretty much it :o)

If you don't have time to worry about it right now, then you don't need to use them for this, as you said. However, it might be something worth remebering later...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.