Assorted C++ Questions

Here are some c++ questions I had written down after reading some books.


1. When would I need to use a body (New scope) inside of a function body?

EXAMPLE:

1
2
3
4
5
6
7
void Function()
{
	cout << “Hello World << endl;
	{
		cout << “Body inside function body” << endl;
	}
}


2. Is there any difference between initializing a variable like this:

int a = 5;

and this?:

int b(2);

3. Is there any reason to use sizeof() besides finding the size of a type?

4. When should I ever use inline?

5. In a struct, what is at the end before the semi colon and why is it there?

1
2
3
4
5
6
7
struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;

bump
Scope: Gosh how I hate dealing with scopes. Anything you declare inside of one scope is inaccessible outside of it.

1
2
3
4
5
6
7
8
9
10
void Function()
{
	cout << “Hello World << endl;
	{
		cout << “Body inside function body” << endl;
                int x = 0; //Let's declare x
	}

    x = 4; //oops, "x" is not in scope, so we cannot access it out here.
}

You don't have to use a "new" body inside a function (with some exceptions - like exceptions or blocks of code (inside a for-loop, an if-statement block, etc)).

2: As far as I know, no. Both will accomplish the same goal (maybe someone with more skill than I posses can answer this in more depth).

3: I have never used "sizeof" other than finding the size of a type. I don't really know of any other reason to use it.

4: Use "inline" when you NEED to increase speed. It replaces the function call with the code of the function. Function calls are costly in terms of time (it takes a "relatively" long time to call a function - passing arguments, waiting for returns, etc), so using "inline" can really increase speed, but it comes at the cost of space. Using "inline" will increase the size of your program (by a lot if you use it a lot or the function is a really long one). You sacrifice one thing for another - it depends on which is more important to you, space or time.

5: Structs. I don't use them - I always define classes (the two are similar, with some differences). object names at the end before the semi colon allows you to declare objects of that struct at the same time you are defining the struct. You do not have to do that, but it is an option.
Some more info on structs: http://www.cplusplus.com/doc/tutorial/structures/
Sizeof() sometimes comes in handy when you want to determine the size of the array. It all depends on what you are doing with it. I know some C++ books that use sizeof() in some games.
hmm, ok thank you very much!

1. You may want to use a local scope inside another one if you want to use resources as short as possible and release them as fast as possible. F.e. a mutex should enclose the shortest possible critical section which may be only some lines of code inside a function body.

2. When using an explicit constructor the first kind of initialization may not work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class A;

ostream &operator<<(ostream &out, const A &a);

class A
{
private:
    int _a;

public:
    A(const A &theA) :
        _a(theA.a())
    {
        clog << "<<copy>> A::A(" << theA << ")" << endl;
    }

    explicit A(int theA = 0) :
        _a(theA)
    {
        clog << "A::A(" << theA << ")" << endl;
    }

    A &operator=(const A &rhs)
    {
        clog << "A::operator=(" << rhs << ")" << endl;
        _a = rhs.a();

        return *this;
    }

    int a() const
    {
        return _a;
    }
 
} /* A */;

ostream &operator<<(ostream &out, const A &a)
{
    return out << a.a();
}

/* ==================================== */
/*
 *  t s t
 */
static void tst(void)
{
    A a(1);
    A b = 7;    // Compilation fails due to missing implicit conversion from 'int' to 'A'

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    b = a;
    cout << "b = a = " << b << endl;

} /* tst() */


I'll give it a try too

#1: as tcs said, it's for short-lived resources; I've used local blocks to scope up std::lock_guards quite often.

#2: the first one is copy-initialization, the second one is direct-initialization. The main difference, as tcs said, is that copy-initialization does not consider explicit constructors/conversion functions. For more detail:
http://en.cppreference.com/w/cpp/language/direct_initialization
http://en.cppreference.com/w/cpp/language/copy_initialization

#3 that's all sizeof does. What would you do with that size can vary from mundane things like sizing up an POD object for memcpy or ostream::write, to, for example, converting a SFINAE-controlled overload to an integer constant at compile time.

#4 use inline when you want to put a non-template function (either non-member or a member defined out-of-line) in a header file, that's what it's for. As for when would you want a non-template function in a header file, opinions vary.

#5 that's a definition of a variable called object_names. Imagine int object_names;, except instead of "int", it says "struct structure_name {}", so it simultaneously defines a type and a variable of that type.

Topic archived. No new replies allowed.