Question about New syntax of C++

  • Context: C/C++ 
  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around the new syntax for class overloading in C++, specifically focusing on class templates, constructors, and exception handling. Participants are examining the differences between the new syntax and the older style, questioning the clarity and simplicity of the new approach.

Discussion Character

  • Debate/contested
  • Technical explanation

Main Points Raised

  • One participant expresses uncertainty about the translation of the constructor and operator[] from new to old syntax, particularly regarding the use of to_string(index) in exception handling.
  • Another participant points out that the old syntax provided does not correctly copy the array, suggesting a correction to the constructor initialization.
  • There is a discussion about the benefits of the new syntax, with one participant questioning how it is considered better or simpler compared to the old syntax.
  • Concerns are raised about the readability of the new syntax, with a participant stating that the old way seems more intuitive and easier to understand.
  • One participant explains the purpose of the constructor member initializer list in the new syntax, emphasizing its role in initializing member variables at the time of their creation.
  • Another participant clarifies that the new syntax does not catch exceptions within the function, contrasting it with the old syntax where exceptions could be handled differently.
  • There is a discussion about the necessity of deleting existing elements in the assignment operator to prevent memory leaks when sizes differ.

Areas of Agreement / Disagreement

Participants express differing views on the clarity and effectiveness of the new syntax compared to the old syntax. There is no consensus on whether the new syntax is indeed better or simpler, as opinions vary significantly.

Contextual Notes

Some participants note that the new syntax introduces complexities that may not be present in the old syntax, leading to confusion about its advantages. The discussion highlights the challenges of transitioning to new programming paradigms.

  • #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; count << "A: " << ka << endl; }
};

int main() {
    count << "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 count 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; count << "A1: " << ka << endl; }
};
struct B1
{
   int kb;
    B1( int k )
    { kb = k; count << "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; count << "A1: " << ka << endl; }
};
struct B1
{
   int kb;
    B1( int k )
    { kb = k; count << "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
3K
Replies
20
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
Replies
89
Views
6K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 45 ·
2
Replies
45
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K