Question about New syntax of C++

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

The discussion centers on the new syntax for class overloading in C++ as presented in Ivor's book, specifically regarding the implementation of constructors and the operator[] function. The user seeks validation of their translation from the new syntax to the old syntax, particularly questioning the use of the member initializer list and exception handling. Key points include the advantages of using size_t over int for size representation and the importance of understanding the order of initialization in constructors. The consensus is that while the new syntax may appear less readable, it offers better memory management and exception handling capabilities.

PREREQUISITES
  • Understanding of C++ class templates
  • Familiarity with exception handling in C++
  • Knowledge of constructor member initializer lists in C++
  • Experience with C++ data types, specifically size_t
NEXT STEPS
  • Study C++11 features, focusing on constructor member initializer lists
  • Learn about exception handling best practices in C++
  • Explore the differences between size_t and int in C++
  • Review C++ class template design patterns for better readability
USEFUL FOR

C++ developers, software engineers transitioning from older C++ syntax, and anyone looking to improve their understanding of modern C++ programming practices.

  • #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