What is the meaning of scoped object in the context below.

Hi,

The Note section of following isocpp guideline,
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c10-prefer-concrete-types-over-class-hierarchies ,

says :


Note The use of indirection is fundamental for run-time polymorphic interfaces. The allocation/deallocation overhead is not (that’s just the most common case). We can use a base class as the interface of a scoped object of a derived class. This is done where dynamic allocation is prohibited (e.g. hard-real-time) and to provide a stable interface to some kinds of plug-ins.


I am literally not able to understand what does the above para means in terms of code implementation.
I would really appreciate if someone can help me with a code example on what does above para is trying to convey.

I am specifically looking for explanations to following :

[1] The use of indirection is fundamental for run-time polymorphic interfaces. The allocation/deallocation overhead is not (that’s just the most common case)
==> Does that mean we can achieve run-time polymorphism without allocation/deallocation ? if yes, how ?

[2] We can use a base class as the interface of a scoped object of a derived class
==> What is a scoped object ? Maybe a code example of 'scoped' and a 'non-scoped' object be really helpful.

[3] 'This' is done where dynamic allocation is prohibited (e.g. hard-real-time) and to provide a stable interface to some kinds of plug-ins
==> What is "This" here?
==> How is "This" achieved without dynamic allocation ? Would really appreciate a code example.

Thanks alot for any help :)
Does that mean we can achieve run-time polymorphism without allocation/deallocation ? if yes, how ?

Maybe like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

struct B {
    virtual ~B() = default;
    // ...default the other special members (omitted for brevity)
    
    virtual void f() const { std::cout << "B::f()\n"; } 
};

struct D: B {
    virtual void f() const override { std::cout << "D::f()\n"; }
};

int main()
{
    D d{};
    B const& b = d;
    b.f(); // prints "D::f()"
}


The relevant property is that some indirection is made - we have to use a pointer or reference. Note that the use of a pointer does not imply dynamic allocation. For example, we can modify the main function to read
1
2
3
4
5
6
int main() 
{
    D d{};
    B const* b = &d;
    b->f(); // prints "D::f()"
}
And retain the same behavior.

What is a scoped object ?

If I'm not mistaken, "scoped object" isn't defined anywhere. I think it's another way to say "an object with automatic storage duration".

If this is correct, in the example above the object d is a scoped object. In B *b = new D, the object *b isn't, since its lifetime is independent.

What is "this" here?

Using a base-class as an interface for an object of a derived class, without dynamic allocation.

For example, the code above uses the interface of B as the interface to an object of a derived class, even though there is no dynamic allocation involved.
Last edited on
Scoped objects are described in R.5.
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r5-prefer-scoped-objects-dont-heap-allocate-unnecessarily
A scoped object is a local object, a global object, or a member. This implies that there is no separate allocation and deallocation cost in excess of that already used for the containing scope or object. The members of a scoped object are themselves scoped and the scoped object’s constructor and destructor manage the members’ lifetimes.
Thanks!
Thanks mbozzi & Peter87 :)
Topic archived. No new replies allowed.