C/C++ Question about New syntax of C++

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
The discussion centers on the new C++ syntax for class template overloading, specifically regarding constructor and operator[] implementations. The user seeks validation on their translation from new to old syntax and expresses confusion over the use of `to_string(index)` in exception handling. They question the benefits of the new syntax, arguing that the old syntax appears clearer and more intuitive. Responses clarify that the new syntax allows for member initialization before the constructor body executes and emphasize the importance of using `size_t` for size representation to avoid overflow issues. The conversation highlights the ongoing debate about readability and clarity in programming syntax.
  • #31
jtbell said:
I think the reason why it's not an error to omit the default constructors, is that in this program, the compiler never actually looks for a default constructor. The member declarations A1 a1; and B1 b1; by themselves do not invoke any constructors. That happens only when T1's constructor is invoked; that constructor explicitly invokes the non-default constructors A1 (int k) and B1 (int k), so the compiler never needs to look for a default constructor A1 () or B1 ().

Try removing either b1( tk ) or a1( tk ) from the initializer list for T1( int k ) and see what happens. This will force the compiler to look for a default constructor.

Here's a simpler program that illustrates the behavior that you're seeing:

C++:
#include <iostream>
using namespace std;

struct A {
    int ka;
    A( int k ) { ka = k; cout << "A: " << ka << endl; }
};

int main() {
    cout << "I'm not constructing any objects of type A." << endl;
    return 0;
}

This compiles successfully for me. Now, in main(), first add A a; which looks for a default constructor (and edit the output to cout if you like :wink: ). Then change it to A a(3); .
Thanks for the reply.

But it sure gave me a compiler error if I write it in my old way. I have to delete it to make it work.
C++:
#include <iostream>
using namespace std;
struct A1
{
    int ka;
    A1( int k)
    { ka = k; cout << "A1: " << ka << endl; }
};
struct B1
{
   int kb;
    B1( int k )
    { kb = k; cout << "B1: " << kb << endl; }
};
struct T1
{
    int tk;
//I cannot put A1 a1; B1 b1; here unless I write a default constructor.
    T1(int k)
    {
        tk = k;
        B1 b1(tk);
        A1 a1(tk);
    }
};
int main()
{
    T1 t1( 3 );
}

Please read line 18. This is a working program, but if I put A1 a1; B1 b1; in line 18, compiler will give me error unless I actually write in the default constructor.

If you look at the new syntax, compiler does not flag an error. That program does not have a default constructor, It is inconsistent. It's the new syntax that is different.

Thanks
 
Technology news on Phys.org
  • #32
yungman said:
Thanks for the reply.

But it sure gave me a compiler error if I write it in my old way. I have to delete it to make it work.
C++:
#include <iostream>
using namespace std;
struct A1
{
    int ka;
    A1( int k)
    { ka = k; cout << "A1: " << ka << endl; }
};
struct B1
{
   int kb;
    B1( int k )
    { kb = k; cout << "B1: " << kb << endl; }
};
struct T1
{
    int tk;
//I cannot put A1 a1; B1 b1; here unless I write a default constructor.
    T1(int k)
    {
        tk = k;
        B1 b1(tk);
        A1 a1(tk);
    }
};
int main()
{
    T1 t1( 3 );
}

Please read line 18. This is a working program, but if I put A1 a1; B1 b1; in line 18, compiler will give me error unless I actually write in the default constructor.

If you look at the new syntax, compiler does not flag an error. That program does not have a default constructor, It is inconsistent. It's the new syntax that is different.

Thanks
In this version, a1 and b1 aren't member variables. That's not a working version. a1, b1 aren't part of T anymore. They're local variables inside T's constructor and are gone once T's constructor finishes.
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
2K
Replies
20
Views
2K
Replies
89
Views
6K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 45 ·
2
Replies
45
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K