为什么我的构造函数没有被调用

时间:2017-03-19 09:10:25

标签: c++ oop

Class A
{
public:
    A()
    {
       cout << "constructor called";
    }
};

int main()
{
  A obj;     // constructor getting called
  A obj1();   // constructor not getting called
}

当我实例化 obj 对象时,我的构造函数被调用。 但是当我实例化 obj1 时,我的构造函数没有被调用。

我想知道它的原因。

1 个答案:

答案 0 :(得分:0)

()实际上没有创建任何对象,它是一个函数声明。

这里应该避免使用

A,因为编译器无法判断您是创建obj1对象还是声明A函数返回 DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("new"); myRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { // This method is called once with the initial value and again // whenever data at this location is updated. // Object key = dataSnapshot.getValue(); if (dataSnapshot.hasChild(ch) ) { Map<String ,Object> key = (Map<String, Object>) dataSnapshot.child(ch).getValue(); sname = (String) key.get("sname"); // Log.v("ABCD ", "Value is: " + key.get("sname")); if(dataSnapshot.getValue() != null) { Intent intent = new Intent("com.example.sunny.new.Selectclass"); startActivity(intent); finish(); } } else{ AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this); builder1.setMessage("Check your Code"); builder1.setCancelable(true); builder1.setPositiveButton( "Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert11 = builder1.create(); alert11.show(); } } @Override public void onCancelled(DatabaseError error) { // Failed to read value Log.v("XYZ", "Failed to read value.", error.toException()); } }); 个对象